/// <summary> /// Clones the week. /// </summary> /// <param name="ownerId">The owner id.</param> /// <param name="srcStartDate">The SRC start date.</param> /// <param name="destStartDate">The dest start date.</param> public static void CloneWeek(int ownerId, DateTime srcStartDate, DateTime destStartDate) { if (!Mediachase.Ibn.License.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); srcStartDate = srcStartDate.Date; destStartDate = destStartDate.Date; TimeTrackingBlock[] srcBlocks = TimeTrackingBlock.List(FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("StartDate", srcStartDate)); using (TransactionScope tran = DataContext.Current.BeginTransaction()) { foreach (TimeTrackingBlock srcBlock in srcBlocks) { TimeTrackingBlock destBlock = new TimeTrackingBlock(); #region Copy TimeTrackingBlock Properties // Set Start Date destBlock.StartDate = destStartDate; // Copy References destBlock.BlockTypeInstanceId = srcBlock.BlockTypeInstanceId; destBlock.mc_StateMachineId = srcBlock.mc_StateMachineId; destBlock.OwnerId = srcBlock.OwnerId; destBlock.ProjectId = srcBlock.ProjectId; // Copy Card destBlock.Card = srcBlock.Card; // Copy Extended properties foreach (MetaObjectProperty exProperty in srcBlock.ExtendedProperties) { if (!exProperty.IsReadOnly) { destBlock.Properties[exProperty.Name].Value = exProperty.Value; } } // Copy Fields destBlock.Title = srcBlock.Title; destBlock.Day1 = 0; destBlock.Day2 = 0; destBlock.Day3 = 0; destBlock.Day4 = 0; destBlock.Day5 = 0; destBlock.Day6 = 0; destBlock.Day7 = 0; #endregion destBlock.Save(); TimeTrackingEntry[] srcEntries = TimeTrackingEntry.List(FilterElement.EqualElement("ParentBlockId", srcBlock.PrimaryKeyId.Value)); foreach (TimeTrackingEntry srcEntry in srcEntries) { TimeTrackingEntry destEntry = new TimeTrackingEntry(); #region Copy TimeTrackingEntry Properties // Set Start Date //destEntry.StartDate = destStartDate; // Referienced Field From TimeTrackingBlock // Copy References destEntry.ParentBlockId = destBlock.PrimaryKeyId.Value; destEntry.BlockTypeInstanceId = srcEntry.BlockTypeInstanceId; destEntry.OwnerId = srcEntry.OwnerId; // Copy Card destEntry.Card = srcEntry.Card; // Copy Extended properties foreach (MetaObjectProperty exProperty in srcEntry.ExtendedProperties) { if (!exProperty.IsReadOnly) { destEntry.Properties[exProperty.Name].Value = exProperty.Value; } } // Copy Fields destEntry.Title = srcEntry.Title; destEntry.Day1 = 0; destEntry.Day2 = 0; destEntry.Day3 = 0; destEntry.Day4 = 0; destEntry.Day5 = 0; destEntry.Day6 = 0; destEntry.Day7 = 0; #endregion destEntry.Save(); } } tran.Commit(); } }
/// <summary> /// Add the TTEntry Item /// </summary> /// <returns></returns> public static TimeTrackingEntry AddEntry(int blockTypeInstanceId, DateTime startDate, int ownerId, string title) { if (!Mediachase.Ibn.License.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); startDate = startDate.Date; TimeTrackingEntry retVal; using (TransactionScope tran = DataContext.Current.BeginTransaction()) { MetaObject blockInstance = MetaObjectActivator.CreateInstance<MetaObject>(BlockTypeInstanceMetaClassName, blockTypeInstanceId); int blockTypeId = (PrimaryKeyId)blockInstance.Properties[BlockTypeInstanceFieldName_BlockType].Value; MetaObject blockType = MetaObjectActivator.CreateInstance<MetaObject>(BlockTypeMetaClassName, blockTypeId); TimeTrackingBlock block = null; TimeTrackingBlock[] mas = TimeTrackingBlock.List(FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("BlockTypeInstanceId", blockTypeInstanceId), FilterElement.EqualElement("StartDate", startDate)); if (mas.Length > 0) { block = mas[0]; if (!Security.CanWrite(block)) throw new AccessDeniedException(); } else { block = new TimeTrackingBlock(); int stateMachineId = (PrimaryKeyId)blockType.Properties[BlockTypeFieldName_StateMachine].Value; string stateName = StateMachineManager.GetStateMachine(BlockMetaClassName, stateMachineId).States[0].Name; MetaObject stateInstance = StateMachineManager.GetState(BlockMetaClassName, stateName); block.mc_StateMachineId = stateMachineId; block.mc_StateId = stateInstance.PrimaryKeyId ?? -1; block.BlockTypeInstanceId = blockTypeInstanceId; block.OwnerId = ownerId; block.StartDate = startDate; block.Title = blockInstance.Properties[BlockTypeFieldName_Title].Value.ToString(); if (blockInstance.Properties[BlockTypeInstanceFieldName_Project].Value != null) block.ProjectId = (PrimaryKeyId)blockInstance.Properties[BlockTypeInstanceFieldName_Project].Value; if (!String.IsNullOrEmpty(blockType.Properties[BlockTypeFieldName_BlockCard].Value.ToString())) block.Properties["Card"].Value = blockType.Properties[BlockTypeFieldName_BlockCard].Value; block.Save(); // Add Owner to Block TimeTrackingManager.AddOwner(block.PrimaryKeyId.Value, ownerId); } TimeTrackingEntry entry = new TimeTrackingEntry(); entry.ParentBlockId = block.PrimaryKeyId ?? -1; entry.Title = title; entry.BlockTypeInstanceId = blockTypeInstanceId; entry.OwnerId = ownerId; if (!String.IsNullOrEmpty(blockType.Properties[BlockTypeFieldName_EntryCard].Value.ToString())) entry.Properties["Card"].Value = blockType.Properties[BlockTypeFieldName_EntryCard].Value; entry.Save(); retVal = entry; tran.Commit(); } return retVal; }
public static void AddEntries(DateTime startDate, int ownerId, List<int> entryIdList) { startDate = startDate.Date; // We use the dictionaries for optimization // whiteList contains the blocks for which we have the "Write" right // blackList contains the blocks for which we don't have the "Write" right Dictionary<int, TimeTrackingBlock> whiteList = new Dictionary<int, TimeTrackingBlock>(); Dictionary<int, TimeTrackingBlock> blackList = new Dictionary<int, TimeTrackingBlock>(); TimeTrackingEntry[] existingEntries = TimeTrackingEntry.List( FilterElement.EqualElement("StartDate", startDate), FilterElement.EqualElement("OwnerId", ownerId)); using (TransactionScope tran = DataContext.Current.BeginTransaction()) { TimeTrackingEntry[] entryList = TimeTrackingEntry.List(new FilterElement("PrimaryKeyId", FilterElementType.In, entryIdList)); foreach (TimeTrackingEntry srcEntry in entryList) { int blockTypeInstanceId = srcEntry.BlockTypeInstanceId; // Go to the next item if we don't have the "Write" right if (blackList.ContainsKey(blockTypeInstanceId)) continue; // Get the block TimeTrackingBlock block; if (whiteList.ContainsKey(blockTypeInstanceId)) { block = whiteList[blockTypeInstanceId]; } else // the block wasn't processed yet { block = GetTimeTrackingBlock(blockTypeInstanceId, startDate, ownerId, true); if (Security.CanWrite(block)) { whiteList.Add(blockTypeInstanceId, block); } else { blackList.Add(blockTypeInstanceId, block); continue; } } // Check for duplicates bool entryExists = false; foreach (TimeTrackingEntry existingEntry in existingEntries) { if (srcEntry.Properties["ObjectId"].Value != null && existingEntry.Properties["ObjectId"].Value != null && (int)srcEntry.Properties["ObjectId"].Value == (int)existingEntry.Properties["ObjectId"].Value && (int)srcEntry.Properties["ObjectTypeId"].Value == (int)existingEntry.Properties["ObjectTypeId"].Value) { entryExists = true; break; } else if (srcEntry.Properties["ObjectId"].Value == null && existingEntry.Properties["ObjectId"].Value == null && srcEntry.Title == existingEntry.Title) { entryExists = true; break; } } if (entryExists) continue; // Add the entry string entryCard = GetEntryCard(blockTypeInstanceId); TimeTrackingEntry entry = new TimeTrackingEntry(); entry.ParentBlockId = block.PrimaryKeyId ?? -1; entry.Title = srcEntry.Title; entry.BlockTypeInstanceId = blockTypeInstanceId; entry.OwnerId = ownerId; entry.Properties["Card"].Value = entryCard; if (srcEntry.Properties["ObjectId"].Value != null && srcEntry.Properties["ObjectTypeId"].Value != null) { entry.Properties["ObjectId"].Value = srcEntry.Properties["ObjectId"].Value; entry.Properties["ObjectTypeId"].Value = srcEntry.Properties["ObjectTypeId"].Value; } entry.Save(); } tran.Commit(); } }
public static void AddEntries(DateTime startDate, int ownerId, List<int> objects, List<int> objectTypes, List<string> titles, List<int> blockTypeInstances) { startDate = startDate.Date; // We use the dictionaries for optimization // whiteList contains the blocks for which we have the "Write" right // blackList contains the blocks for which we don't have the "Write" right // entryCardList contains card names by BlockTypeInstanceId Dictionary<int, TimeTrackingBlock> whiteList = new Dictionary<int, TimeTrackingBlock>(); Dictionary<int, TimeTrackingBlock> blackList = new Dictionary<int, TimeTrackingBlock>(); Dictionary<int, string> entryCardList = new Dictionary<int, string>(); // Get the list of existing entries TimeTrackingEntry[] existingEntries = TimeTrackingEntry.List( FilterElement.EqualElement("StartDate", startDate), FilterElement.EqualElement("OwnerId", ownerId), FilterElement.IsNotNullElement("ObjectId")); using (TransactionScope tran = DataContext.Current.BeginTransaction()) { for (int i = 0; i < objects.Count; i++) { int blockTypeInstanceId = blockTypeInstances[i]; // Go to the next item if we don't have the "Write" right if (blackList.ContainsKey(blockTypeInstanceId)) continue; // Get the block TimeTrackingBlock block; if (whiteList.ContainsKey(blockTypeInstanceId)) { block = whiteList[blockTypeInstanceId]; } else // new block { block = GetTimeTrackingBlock(blockTypeInstanceId, startDate, ownerId, true); if (Security.CanWrite(block)) { whiteList.Add(blockTypeInstanceId, block); } else { blackList.Add(blockTypeInstanceId, block); continue; } } // Check for duplicates bool entryExists = false; foreach (TimeTrackingEntry existingEntry in existingEntries) { if (objects[i] == (int)existingEntry.Properties["ObjectId"].Value && objectTypes[i] == (int)existingEntry.Properties["ObjectTypeId"].Value) { entryExists = true; break; } } if (entryExists) continue; // Get the entry card string entryCard; if (entryCardList.ContainsKey(blockTypeInstanceId)) { entryCard = entryCardList[blockTypeInstanceId]; } else { entryCard = GetEntryCard(blockTypeInstanceId); entryCardList.Add(blockTypeInstanceId, entryCard); } // Adding TimeTrackingEntry entry = new TimeTrackingEntry(); entry.ParentBlockId = block.PrimaryKeyId ?? -1; entry.Title = titles[i]; entry.BlockTypeInstanceId = blockTypeInstanceId; entry.OwnerId = ownerId; entry.Properties["Card"].Value = entryCard; entry.Properties["ObjectId"].Value = objects[i]; entry.Properties["ObjectTypeId"].Value = objectTypes[i]; entry.Save(); } tran.Commit(); } }
/// <summary> /// Adds the TimeTracking entries /// </summary> /// <param name="blockTypeInstanceId"></param> /// <param name="startDate"></param> /// <param name="ownerId"></param> /// <param name="objects"></param> /// <param name="objectTypes"></param> /// <param name="titles"></param> public static void AddEntries(int blockTypeInstanceId, DateTime startDate, int ownerId, List<int> objects, List<int> objectTypes, List<string> titles) { if (!Mediachase.Ibn.License.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); startDate = startDate.Date; // Get the list of existing entries TimeTrackingEntry[] existingEntries = TimeTrackingEntry.List( FilterElement.EqualElement("StartDate", startDate), FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("BlockTypeInstanceId", blockTypeInstanceId), FilterElement.IsNotNullElement("ObjectId")); using (TransactionScope tran = DataContext.Current.BeginTransaction()) { // Get the block TimeTrackingBlock block = GetTimeTrackingBlock(blockTypeInstanceId, startDate, ownerId, true); if (!Security.CanWrite(block)) throw new AccessDeniedException(); string entryCard = GetEntryCard(blockTypeInstanceId); // Loop by entries for (int i = 0; i < objects.Count; i++) { // Check for duplicates bool entryExists = false; foreach (TimeTrackingEntry existingEntry in existingEntries) { if (objects[i] == (int)existingEntry.Properties["ObjectId"].Value && objectTypes[i] == (int)existingEntry.Properties["ObjectTypeId"].Value) { entryExists = true; break; } } if (entryExists) continue; // Add the entry TimeTrackingEntry entry = new TimeTrackingEntry(); entry.ParentBlockId = block.PrimaryKeyId ?? -1; entry.Title = titles[i]; entry.BlockTypeInstanceId = blockTypeInstanceId; entry.OwnerId = ownerId; entry.Properties["Card"].Value = entryCard; entry.Properties["ObjectId"].Value = objects[i]; entry.Properties["ObjectTypeId"].Value = objectTypes[i]; entry.Save(); } tran.Commit(); } }
/// <summary> /// Updates the TTEntry Item /// </summary> /// <returns></returns> public static void UpdateEntry(int entryId, int totalApproved, decimal rate) { if (!Mediachase.Ibn.License.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); using (TransactionScope tran = DataContext.Current.BeginTransaction()) { TimeTrackingEntry entry = new TimeTrackingEntry(entryId); TimeTrackingBlock ttb = MetaObjectActivator.CreateInstance<TimeTrackingBlock>(TimeTrackingBlock.GetAssignedMetaClass(), entry.ParentBlockId); if (!Security.CheckObjectRight(ttb, Right_RegFinances)) throw new AccessDeniedException(); using (SkipSecurityCheckScope skipSecurity = Security.SkipSecurityCheck()) { entry.Properties["TotalApproved"].Value = (double)totalApproved; entry.Properties["Rate"].Value = rate; entry.Save(); } tran.Commit(); } }