}//threadCallBack()

        private bool _doesGeneratorRunNow(CswNbtObjClassGenerator CurrentGenerator)
        {
            bool     RunNow              = false;
            DateTime ThisDueDateValue    = CurrentGenerator.NextDueDate.DateTimeValue.Date;
            DateTime InitialDueDateValue = CurrentGenerator.DueDateInterval.getStartDate().Date;

            if (InitialDueDateValue == DateTime.MinValue)
            {
                InitialDueDateValue = ThisDueDateValue;
            }
            DateTime FinalDueDateValue = CurrentGenerator.FinalDueDate.DateTimeValue.Date;

            if (ThisDueDateValue != DateTime.MinValue)
            {
                if (CurrentGenerator.RunTime.DateTimeValue != DateTime.MinValue &&
                    CswEnumRateIntervalType.Hourly != CurrentGenerator.DueDateInterval.RateInterval.RateType)  // Ignore runtime for hourly generators
                {
                    ThisDueDateValue = ThisDueDateValue.AddTicks(CurrentGenerator.RunTime.DateTimeValue.TimeOfDay.Ticks);
                }

                Int32 WarnDays = CswConvert.ToInt32(CurrentGenerator.WarningDays.Value);
                if (WarnDays > 0)
                {
                    TimeSpan WarningDaysSpan = new TimeSpan(WarnDays, 0, 0, 0, 0);
                    ThisDueDateValue    = ThisDueDateValue.Subtract(WarningDaysSpan);
                    InitialDueDateValue = InitialDueDateValue.Subtract(WarningDaysSpan);
                }

                // if we're within the initial and final due dates, but past the current due date (- warning days) and runtime
                RunNow = (DateTime.Now.Date >= InitialDueDateValue) &&
                         (DateTime.Now.Date <= FinalDueDateValue || DateTime.MinValue.Date == FinalDueDateValue) &&
                         (DateTime.Now >= ThisDueDateValue);
            }
            return(RunNow);
        }
Beispiel #2
0
        } // update()

        private void _fixBadScheudlesAndTasks()
        {
            ICswNbtTree BadSchedulesTree = _CswNbtSchemaModTrnsctn.getTreeFromView( _getBadSchedulesAndTasksView(), false );
            BadSchedulesTree.goToRoot();
            for( int i = 0; i < BadSchedulesTree.getChildNodeCount(); i++ )
            {
                BadSchedulesTree.goToNthChild( i );

                CswNbtObjClassGenerator ScheduleNode = BadSchedulesTree.getNodeForCurrentPosition();
                int TargetTypeId = ScheduleNode.NodeTypeId == AssemblyScheduleNT.NodeTypeId ? AssemblyTaskNT.NodeTypeId : EquipmentTaskNT.NodeTypeId;
                ScheduleNode.TargetType.SelectedNodeTypeIds.Clear();
                ScheduleNode.TargetType.SelectedNodeTypeIds.Add( CswConvert.ToString( TargetTypeId ) );
                ScheduleNode.postChanges( false );

                for( int j = 0; j < BadSchedulesTree.getChildNodeCount(); j++ )
                {
                    BadSchedulesTree.goToNthChild( j );

                    CswNbtNode OldTaskNode = BadSchedulesTree.getNodeForCurrentPosition();
                    CswNbtObjClassTask NewTaskNode = _CswNbtSchemaModTrnsctn.Nodes.makeNodeFromNodeTypeId( TargetTypeId, CswEnumNbtMakeNodeOperation.WriteNode );
                    NewTaskNode.Node.copyPropertyValues( OldTaskNode );
                    NewTaskNode.Owner.RelatedNodeId = ScheduleNode.Owner.RelatedNodeId;
                    OldTaskNode.delete();
                    NewTaskNode.postChanges( false );

                    BadSchedulesTree.goToParentNode();
                }

                BadSchedulesTree.goToParentNode();
            }
        }
        public void updateNextDueDateTest_ExplicitUpdate()
        {
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.WeeklyByDay);

            GeneratorNode.updateNextDueDate(ForceUpdate: false, DeleteFutureNodes: false);
            Assert.AreEqual(getNextDay(DayOfWeek.Monday), GeneratorNode.NextDueDate.DateTimeValue);
        }
Beispiel #4
0
        internal CswNbtNode createGeneratorNode(CswEnumRateIntervalType IntervalType, String NodeTypeName = "Equipment Schedule", int WarningDays = 0, SortedList Days = null, DateTime?StartDate = null)
        {
            CswNbtNode ret = _CswNbtResources.Nodes.makeNodeFromNodeTypeId(_getNodeTypeId(NodeTypeName), delegate(CswNbtNode NewNode)
            {
                CswNbtObjClassGenerator GeneratorNode = NewNode;
                CswRateInterval RateInt = new CswRateInterval(_CswNbtResources);
                DateTime StDate         = StartDate != null ? (DateTime)StartDate : new DateTime(2012, 1, 15);
                if (IntervalType == CswEnumRateIntervalType.WeeklyByDay)
                {
                    if (null == Days)
                    {
                        Days = new SortedList {
                            { DayOfWeek.Monday, DayOfWeek.Monday }
                        };
                    }
                    RateInt.setWeeklyByDay(Days, StDate);
                }
                else if (IntervalType == CswEnumRateIntervalType.MonthlyByDate)
                {
                    RateInt.setMonthlyByDate(1, StDate.Day, StDate.Month, StDate.Year);
                }
                GeneratorNode.DueDateInterval.RateInterval = RateInt;
                GeneratorNode.WarningDays.Value            = WarningDays;
                //GeneratorNode.postChanges( ForceUpdate: false );
            });

            _finalize();

            return(ret);
        } // createGeneratorNode()
        public void updateNextDueDateTest_DeleteFutureMonthly()
        {
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.MonthlyByDate);
            CswNbtObjClassGenerator ExistingGen   = TestData.CswNbtResources.Nodes[GeneratorNode.NodeId];

            ExistingGen.updateNextDueDate(ForceUpdate: false, DeleteFutureNodes: true);
            Assert.AreEqual(getNextDate(15), ExistingGen.NextDueDate.DateTimeValue);
        }
        public void updateNextDueDateTest_ExplicitSet()
        {
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.WeeklyByDay);

            GeneratorNode.NextDueDate.DateTimeValue = DateTime.Today;
            GeneratorNode.postChanges(true);
            Assert.AreEqual(DateTime.Today, GeneratorNode.NextDueDate.DateTimeValue);
        }
        public void updateNextDueDateTest_AWeekFromToday()
        {
            DayOfWeek Today = DateTime.Today.DayOfWeek;
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.WeeklyByDay, Days: new SortedList {
                { Today, Today }
            }, StartDate: DateTime.Today);

            Assert.AreEqual(DateTime.Today.AddDays(7), GeneratorNode.NextDueDate.DateTimeValue);
        }
        }     // makeBatchOp()

        /// <summary>
        /// Run the next iteration of this batch operation
        /// </summary>
        public void runBatchOp(CswNbtObjClassBatchOp BatchNode)
        {
            try
            {
                if (BatchNode != null && BatchNode.OpNameValue == CswEnumNbtBatchOpName.FutureNodes)
                {
                    BatchNode.start();

                    FutureNodesBatchData BatchData = new FutureNodesBatchData(BatchNode.BatchData.Text);
                    if (BatchData.GeneratorNodeId != null && BatchData.NextStartDate != DateTime.MinValue)
                    {
                        CswNbtNode GenNode = _CswNbtResources.Nodes[BatchData.GeneratorNodeId];
                        if (null != GenNode)
                        {
                            CswNbtObjClassGenerator GeneratorNode = (CswNbtObjClassGenerator)GenNode;
                            DateTime ThisDate = BatchData.NextStartDate;

                            CswNbtActGenerateNodes CswNbtActGenerateNodes = new CswNbtActGenerateNodes(_CswNbtResources);
                            CswNbtActGenerateNodes.MarkFuture = true;

                            // Run this iteration
                            bool Finished = false;
                            if (ThisDate != DateTime.MinValue &&
                                ThisDate.Date <= BatchData.FinalDate.Date &&
                                (GeneratorNode.FinalDueDate.Empty || ThisDate.Date <= GeneratorNode.FinalDueDate.DateTimeValue.Date))
                            {
                                Finished = CswNbtActGenerateNodes.makeNode(GenNode, ThisDate);
                                //BatchNode.appendToLog( "Created future task for " + ThisDate.ToShortDateString() + "." );
                            }
                            else
                            {
                                BatchNode.finish();
                            }

                            // Setup for next iteration
                            if (Finished)
                            {
                                BatchData.NextStartDate = GeneratorNode.DueDateInterval.getNextOccuranceAfter(ThisDate);
                                if (BatchData.NextStartDate.Date == ThisDate.Date)  // infinite loop guard
                                {
                                    BatchNode.finish();
                                }
                                BatchData.IterationCount += 1;
                            }
                            BatchNode.BatchData.Text    = BatchData.ToString();
                            BatchNode.PercentDone.Value = getPercentDone(BatchNode);
                        } // if( null != GenNode )
                    }     // if( _BatchData.GeneratorNodeId != null && _BatchData.NextStartDate != DateTime.MinValue )

                    BatchNode.postChanges(false);
                } // if( BatchNode != null && BatchNode.OpNameValue == NbtBatchOpName.FutureNodes )
            }
            catch (Exception ex)
            {
                BatchNode.error(ex);
            }
        } // runBatchOp()
        private CswNbtNode _getTargetNodeForGenerator(CswNbtNode CswNbtNodeGenerator, CswPrimaryKey ParentPk, string TargetDateFilter)
        {
            CswNbtNode ReturnVal = null;

            CswNbtObjClassGenerator GeneratorNode = (CswNbtObjClassGenerator)CswNbtNodeGenerator;

            if (String.IsNullOrEmpty(GeneratorNode.TargetType.SelectedNodeTypeIds.ToString()))
            {
                throw new CswDniException(CswEnumErrorType.Error, "Invalid generator configuration", "_getTargetNodeForGenerator got a null SelectedNodeTypeIds on nodeid " + GeneratorNode.NodeId);
            }
            else
            {
                Int32 NodeTypeId = CswConvert.ToInt32(GeneratorNode.TargetType.SelectedNodeTypeIds[0]);   // case 28612 - just check the first one
                if (Int32.MinValue != NodeTypeId)
                {
                    CswNbtMetaDataNodeType ThisCreatedNodeType = _CswNbtResources.MetaData.getNodeType(NodeTypeId);
                    if (null != ThisCreatedNodeType)
                    {
                        CswNbtMetaDataNodeType    CreatedNodeType            = ThisCreatedNodeType.getNodeTypeLatestVersion();
                        CswNbtMetaDataObjectClass CreatedMetaDataObjectClass = CreatedNodeType.getObjectClass();

                        CswNbtObjClass CreatedObjClass = CswNbtObjClassFactory.makeObjClass(_CswNbtResources, CreatedMetaDataObjectClass);
                        if (!(CreatedObjClass is CswNbtPropertySetGeneratorTarget))
                        {
                            throw new CswDniException("CswNbtActGenerateNodes got an invalid object class: " + CreatedObjClass.ObjectClass.ToString());
                        }
                        CswNbtPropertySetGeneratorTarget GeneratorTarget = (CswNbtPropertySetGeneratorTarget)CreatedObjClass;

                        // CreatedForNTP is the parent or owner of the new node. Inspections created for Inspection Targets, Tasks for Equipment, etc.
                        CswNbtMetaDataNodeTypeProp CreatedForNTP = CreatedNodeType.getNodeTypePropByObjectClassProp(GeneratorTarget.ParentPropertyName);
                        CswNbtMetaDataNodeTypeProp GeneratorNTP  = CreatedNodeType.getNodeTypePropByObjectClassProp(CswNbtPropertySetGeneratorTarget.PropertyName.Generator);
                        CswNbtMetaDataNodeTypeProp DueDateNTP    = CreatedNodeType.getNodeTypePropByObjectClassProp(CswNbtPropertySetGeneratorTarget.PropertyName.DueDate);

                        CswNbtView CswNbtView = new CswNbtView(_CswNbtResources);
                        CswNbtView.ViewName = "Nodes for Generator";

                        CswNbtViewRelationship RootRelationship     = CswNbtView.AddViewRelationship(CreatedNodeType, false);
                        CswNbtViewProperty     CreatedForParentProp = CswNbtView.AddViewProperty(RootRelationship, CreatedForNTP);
                        CswNbtView.AddViewPropertyFilter(CreatedForParentProp, CswNbtFieldTypeRuleRelationship.SubFieldName.NodeID, CswEnumNbtFilterMode.Equals, ParentPk.PrimaryKey.ToString(), false);
                        CswNbtViewProperty GeneratorProp = CswNbtView.AddViewProperty(RootRelationship, GeneratorNTP);
                        CswNbtView.AddViewPropertyFilter(GeneratorProp, CswNbtFieldTypeRuleRelationship.SubFieldName.NodeID, CswEnumNbtFilterMode.Equals, CswNbtNodeGenerator.NodeId.PrimaryKey.ToString(), false);
                        CswNbtViewProperty DueDateProp = CswNbtView.AddViewProperty(RootRelationship, DueDateNTP);
                        //Case 24572
                        CswNbtView.AddViewPropertyFilter(DueDateProp, CswNbtFieldTypeRuleDateTime.SubFieldName.Value, CswEnumNbtFilterMode.Equals, TargetDateFilter, false);

                        ICswNbtTree ExistingNodesTree = _CswNbtResources.Trees.getTreeFromView(_CswNbtResources.CurrentNbtUser, CswNbtView, true, false, false);

                        if (ExistingNodesTree.getChildNodeCount() > 0)
                        {
                            ExistingNodesTree.goToNthChild(0);
                            ReturnVal = ExistingNodesTree.getNodeForCurrentPosition();
                        }
                    }
                }
            }
            return(ReturnVal);
        }//_getTargetNodeForGenerator
        } // makeBatchOp()

        /// <summary>
        /// Create a new batch operation to handle future node generation
        /// </summary>
        /// <param name="GenNode">Generator Node</param>
        /// <param name="FinalDate"></param>
        public CswNbtObjClassBatchOp makeBatchOp(CswNbtNode GenNode, DateTime FinalDate)
        {
            CswNbtObjClassBatchOp BatchNode = null;

            if (null != GenNode)
            {
                CswNbtObjClassGenerator GeneratorNode = (CswNbtObjClassGenerator)GenNode;


                // BZ 6752 - The first future node is the first node generated
                // after today + warning days, according to the time interval
                // But it has to include initial due date, no matter what the time interval.

                CswNbtNodePropTimeInterval NextDueDateTimeInterval = GeneratorNode.DueDateInterval;
                Double WarningDays = 0;
                if (GeneratorNode.WarningDays.Value > 0)
                {
                    WarningDays = GeneratorNode.WarningDays.Value;
                }
                DateTime StartDate = DateTime.Now.AddDays(WarningDays).Date;   //bz# 6937 (capture date only, not time)

                DateTime DateOfNextOccurance = DateTime.MinValue;
                if (GeneratorNode.DueDateInterval.getStartDate().Date >= StartDate)  //bz # 6937 (change gt to gteq)
                {
                    DateOfNextOccurance = GeneratorNode.DueDateInterval.getStartDate().Date;
                }
                else
                {
                    DateOfNextOccurance = NextDueDateTimeInterval.getNextOccuranceAfter(StartDate);
                }

                // Determine number of iterations
                Int32    StartingCount = 0;
                DateTime ThisDate      = DateOfNextOccurance;

                while (ThisDate != DateTime.MinValue &&
                       ThisDate.Date <= FinalDate &&
                       (GeneratorNode.FinalDueDate.Empty || ThisDate.Date <= GeneratorNode.FinalDueDate.DateTimeValue.Date))
                {
                    StartingCount++;
                    ThisDate = GeneratorNode.DueDateInterval.getNextOccuranceAfter(ThisDate);
                }

                FutureNodesBatchData BatchData = new FutureNodesBatchData(string.Empty);
                BatchData.GeneratorNodeId = GenNode.NodeId;
                BatchData.NextStartDate   = DateOfNextOccurance;
                BatchData.FinalDate       = FinalDate;
                BatchData.StartingCount   = StartingCount;
                BatchData.IterationCount  = 0;

                BatchNode = CswNbtBatchManager.makeNew(_CswNbtResources, _BatchOpName, BatchData.ToString());
            } // if(null != GeneratorNode)
            return(BatchNode);
        }     // makeBatchOp()
        public void updateNextDueDateTest_FinalDueDateBeforeNextDueDate()
        {
            DayOfWeek FiveDaysFromNow             = DateTime.Today.AddDays(5).DayOfWeek;
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.WeeklyByDay, Days: new SortedList {
                { FiveDaysFromNow, FiveDaysFromNow }
            }, WarningDays: 1);
            CswNbtObjClassGenerator ExistingGen = TestData.CswNbtResources.Nodes[GeneratorNode.NodeId];

            ExistingGen.FinalDueDate.DateTimeValue = DateTime.Today;
            ExistingGen.postChanges(false);
            Assert.AreEqual(DateTime.MinValue, ExistingGen.NextDueDate.DateTimeValue);
        }
        public void updateNextDueDateTest_NextDueDateEqualsStartDate()
        {
            DayOfWeek ThreeDaysFromNow            = DateTime.Today.AddDays(3).DayOfWeek;
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.WeeklyByDay, Days: new SortedList {
                { ThreeDaysFromNow, ThreeDaysFromNow }
            }, StartDate: DateTime.Today.AddDays(3), WarningDays: 5);

            Assert.AreEqual(GeneratorNode.DueDateInterval.getStartDate(), GeneratorNode.NextDueDate.DateTimeValue);
            CswNbtObjClassGenerator ExistingGen = TestData.CswNbtResources.Nodes[GeneratorNode.NodeId];

            ExistingGen.updateNextDueDate(ForceUpdate: true, DeleteFutureNodes: false);
            Assert.AreEqual(DateTime.Today.AddDays(10), ExistingGen.NextDueDate.DateTimeValue);
        }
Beispiel #13
0
        public CswNbtView getTreeViewOfFutureNodes(IEnumerable GeneratorNodes)
        {
            CswNbtView ReturnVal = new CswNbtView(_CswNbtResources);

            ReturnVal.ViewName = "All Future Nodes";
            CswNbtMetaDataObjectClass GeneratorObjectClass  = _CswNbtResources.MetaData.getObjectClass(CswEnumNbtObjectClass.GeneratorClass);
            CswNbtViewRelationship    GeneratorRelationship = ReturnVal.AddViewRelationship(GeneratorObjectClass, false);

            ArrayList TargetNodeTypeIds = new ArrayList();

            foreach (CswNbtNode CurrentGeneratorNode in GeneratorNodes)
            {
                GeneratorRelationship.NodeIdsToFilterIn.Add(CurrentGeneratorNode.NodeId);
                CswNbtObjClassGenerator Generator = CurrentGeneratorNode;
                foreach (String nodeTypeId in Generator.TargetType.SelectedNodeTypeIds)
                {
                    Int32 CurrentTargetNodeTypeId = CswConvert.ToInt32(nodeTypeId);
                    if (!(TargetNodeTypeIds.Contains(CurrentTargetNodeTypeId)))
                    {
                        TargetNodeTypeIds.Add(CurrentTargetNodeTypeId);
                    }
                }
            }

            foreach (Int32 TargetNodeTypeId in TargetNodeTypeIds)
            {
                CswNbtMetaDataNodeType TargetNodeType = _CswNbtResources.MetaData.getNodeType(TargetNodeTypeId);
                if (TargetNodeType != null)
                {
                    CswNbtMetaDataObjectClass TargetObjectClass = TargetNodeType.getObjectClass();

                    CswNbtObjClass TargetObjClass = CswNbtObjClassFactory.makeObjClass(_CswNbtResources, TargetObjectClass);
                    if (!(TargetObjClass is CswNbtPropertySetGeneratorTarget))
                    {
                        throw new CswDniException("CswNbtActGenerateFutureNodes.getTreeViewOfFutureNodes() got an invalid object class: " + TargetObjectClass.ObjectClass);
                    }

                    CswNbtViewRelationship   TargetRelationship   = ReturnVal.AddViewRelationship(GeneratorRelationship, CswEnumNbtViewPropOwnerType.Second, TargetNodeType.getNodeTypePropByObjectClassProp(CswNbtPropertySetGeneratorTarget.PropertyName.Generator), false);
                    CswNbtViewProperty       IsFutureFlagProperty = ReturnVal.AddViewProperty(TargetRelationship, TargetNodeType.getNodeTypePropByObjectClassProp(CswNbtPropertySetGeneratorTarget.PropertyName.IsFuture));
                    CswNbtViewPropertyFilter IsFutureFilter       = ReturnVal.AddViewPropertyFilter(IsFutureFlagProperty, CswNbtFieldTypeRuleLogical.SubFieldName.Checked, CswEnumNbtFilterMode.Equals, CswEnumTristate.True, false);
                }
            }

            return(ReturnVal);
        }//getTreeViewOfFutureNodes()
Beispiel #14
0
        public CswNbtObjClassBatchOp makeNodesBatch(CswNbtNode CswNbtNodeGenerator, DateTime FutureDate)
        {
            CswNbtObjClassBatchOp   BatchNode     = null;
            CswNbtObjClassGenerator GeneratorNode = CswNbtNodeGenerator;
            Int32 TargetNodeTypeId = CswConvert.ToInt32(GeneratorNode.TargetType.SelectedNodeTypeIds);

            // Must have create permissions on this generator's target's nodetype
            if (_CswNbtResources.Permit.canNodeType(CswEnumNbtNodeTypePermission.Create, _CswNbtResources.MetaData.getNodeType(TargetNodeTypeId)))
            {
                deleteExistingFutureNodes(CswNbtNodeGenerator);

                // Send to background task
                CswNbtBatchOpFutureNodes BatchOp = new CswNbtBatchOpFutureNodes(_CswNbtResources);
                BatchNode = BatchOp.makeBatchOp(CswNbtNodeGenerator, FutureDate);
            }

            return(BatchNode);
        }
        public void updateNextDueDateTest_ForceUpdateRevert()
        {
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.MonthlyByDate);
            CswNbtObjClassGenerator ExistingGen   = TestData.CswNbtResources.Nodes[GeneratorNode.NodeId];

            ExistingGen.updateNextDueDate(ForceUpdate: true, DeleteFutureNodes: false);
            ExistingGen.postChanges(true);
            Assert.AreEqual(getNextDate(15).AddMonths(1), ExistingGen.NextDueDate.DateTimeValue);
            DateTime LastDueDate = ExistingGen.DueDateInterval.getLastOccuranceBefore(ExistingGen.NextDueDate.DateTimeValue);

            if (LastDueDate > DateTime.Today)
            {
                ExistingGen.NextDueDate.DateTimeValue = DateTime.Now;
                ExistingGen.updateNextDueDate(ForceUpdate: true, DeleteFutureNodes: false);
                ExistingGen.postChanges(true);
            }
            Assert.AreEqual(getNextDate(15), ExistingGen.NextDueDate.DateTimeValue);
        }
        public void updateNextDueDateTest_FutureStartDateEnforced()
        {
            DateTime StartDate = DateTime.Today.AddDays(10);
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(
                CswEnumRateIntervalType.WeeklyByDay,
                Days: new SortedList
            {
                { DayOfWeek.Monday, DayOfWeek.Monday },
                { DayOfWeek.Wednesday, DayOfWeek.Wednesday },
                { DayOfWeek.Friday, DayOfWeek.Friday }
            },
                WarningDays: 0,
                StartDate: StartDate);
            CswNbtObjClassGenerator ExistingGen = TestData.CswNbtResources.Nodes[GeneratorNode.NodeId];

            Assert.IsTrue(ExistingGen.NextDueDate.DateTimeValue >= StartDate,
                          "NextDueDate (" + ExistingGen.NextDueDate.DateTimeValue.ToShortDateString() + ") is not greater than StartDate (" + StartDate.ToShortDateString() + ").");
        }
 private void _processGenerator(CswNbtResources CswNbtResources, CswNbtObjClassGenerator CurrentGenerator)
 {
     try
     {
         // case 28069
         // It should not be possible to make more than 24 nodes per parent in a single day,
         // since the fastest interval is 1 hour, and we're not creating things into the past anymore.
         // Therefore, disable anything that is erroneously spewing things.
         if (CurrentGenerator.GeneratedNodeCount(DateTime.Today) >= (24 * CurrentGenerator.TargetParents.Count))
         {
             string Message = "Disabled due to error: Generated too many " + CurrentGenerator.TargetType.SelectedNodeTypeNames() + " targets in a single day";
             _StatusMessage += Message + "; ";
             CurrentGenerator.Enabled.Checked = CswEnumTristate.False;
             CurrentGenerator.RunStatus.AddComment(Message);
             CurrentGenerator.postChanges(false);
         }
         else
         {
             CswNbtActGenerateNodes CswNbtActGenerateNodes = new CswNbtActGenerateNodes(CswNbtResources);
             bool Finished = CswNbtActGenerateNodes.makeNode(CurrentGenerator.Node);
             if (Finished)  // case 26111
             {
                 string Message = "Created all " + CurrentGenerator.TargetType.SelectedNodeTypeNames() + " target(s) for " + CurrentGenerator.NextDueDate.DateTimeValue.Date.ToShortDateString();
                 CurrentGenerator.RunStatus.AddComment(Message);
                 CurrentGenerator.updateNextDueDate(ForceUpdate: true, DeleteFutureNodes: false);
                 CurrentGenerator.postChanges(false);
             }
             _StatusMessage += CurrentGenerator.Description.Text + "; ";
         } // if-else( CurrentGenerator.GeneratedNodeCount( DateTime.Today ) >= 24 )
     }     //try
     catch (Exception Exception)
     {
         string Message = "Unable to process generator " + CurrentGenerator.Description.Text + ", which will now be disabled, due to the following exception: " + Exception.Message;
         _StatusMessage += Message + "; ";
         CurrentGenerator.Enabled.Checked = CswEnumTristate.False;
         CurrentGenerator.RunStatus.AddComment("Disabled due to exception: " + Exception.Message);
         CurrentGenerator.postChanges(false);
         CswNbtResources.logError(new CswDniException(Message));
     }
 }
        /// <summary>
        /// Generates a future IGeneratorTarget node.  If an existing node has the same due date, no node is generated.
        /// </summary>
        /// <returns>True if a future node was generated</returns>
        public bool makeNode(CswNbtNode CswNbtNodeGenerator, DateTime DueDate)
        {
            Int32 NodesCreated = 0;

            CswNbtObjClassGenerator GeneratorNodeAsGenerator = (CswNbtObjClassGenerator)CswNbtNodeGenerator;

            string SelectedNodeTypeIdStr = string.Empty;
            Int32  SelectedNodeTypeId    = Int32.MinValue;

            if (0 < GeneratorNodeAsGenerator.TargetType.SelectedNodeTypeIds.Count)
            {
                SelectedNodeTypeIdStr = GeneratorNodeAsGenerator.TargetType.SelectedNodeTypeIds[0];
                SelectedNodeTypeId    = CswConvert.ToInt32(SelectedNodeTypeIdStr);
            }

            if (string.IsNullOrEmpty(SelectedNodeTypeIdStr) ||
                "0" == SelectedNodeTypeIdStr ||
                Int32.MinValue == SelectedNodeTypeId ||
                null == _CswNbtResources.MetaData.getNodeType(SelectedNodeTypeId))
            {
                throw (new CswDniException("Generator node " + CswNbtNodeGenerator.NodeName + " (" + CswNbtNodeGenerator.NodeId.ToString() + ") does not have a valid nodetypeid"));
            }

            if (DueDate == DateTime.MinValue)
            {
                DueDate = GeneratorNodeAsGenerator.NextDueDate.DateTimeValue.Date;
            }
            if (DueDate == DateTime.MinValue)
            {
                DueDate = GeneratorNodeAsGenerator.DueDateInterval.getStartDate().Date;
            }
            string DateFilter = DueDate.ToShortDateString();

            bool GeneratorBaseIsProperlyConfigured = (null != GeneratorNodeAsGenerator.Owner &&
                                                      null != GeneratorNodeAsGenerator.Owner.RelatedNodeId &&
                                                      null != _CswNbtResources.Nodes.GetNode(GeneratorNodeAsGenerator.Owner.RelatedNodeId) &&
                                                      GeneratorNodeAsGenerator.TargetType.SelectedNodeTypeIds.Count > 0);

            if (false == GeneratorBaseIsProperlyConfigured)
            {
                throw new CswDniException(CswEnumErrorType.Error, "Cannot execute generator task if the generator does not have an owner and a target type.", "Generator node did not define both an Owner and a Target Type.");
            }

            // case 26111 - only generate a few at a time, and only increment NextDueDate when we're completely done
            Int32 GeneratorTargetLimit = CswConvert.ToInt32(_CswNbtResources.ConfigVbls.getConfigVariableValue(CswEnumNbtConfigurationVariables.generatortargetlimit.ToString()));

            if (Int32.MinValue == GeneratorTargetLimit)
            {
                GeneratorTargetLimit = 5;
            }

            foreach (CswPrimaryKey NewParentPk in GeneratorNodeAsGenerator.TargetParents)
            {
                if (null != NewParentPk && NodesCreated < GeneratorTargetLimit)
                {
                    CswNbtPropertySetGeneratorTarget ExistingNode = null;
                    if (GeneratorNodeAsGenerator.DueDateInterval.RateInterval.RateType != CswEnumRateIntervalType.Hourly)
                    {
                        ExistingNode = _getTargetNodeForGenerator(CswNbtNodeGenerator, NewParentPk, DateFilter);
                    }
                    if (null == ExistingNode)
                    {
                        Collection <Int32> SelectedNodeTypeIds = GeneratorNodeAsGenerator.TargetType.SelectedNodeTypeIds.ToIntCollection();
                        foreach (Int32 refNodeTypeId in SelectedNodeTypeIds)
                        {
                            CswNbtMetaDataNodeType LatestVersionNT = _CswNbtResources.MetaData.getNodeType(refNodeTypeId).getNodeTypeLatestVersion();

                            //CswNbtPropertySetGeneratorTarget NewNode = _CswNbtResources.Nodes.makeNodeFromNodeTypeId( LatestVersionNT.NodeTypeId, CswEnumNbtMakeNodeOperation.DoNothing );
                            //NewNode.Node.copyPropertyValues( CswNbtNodeGenerator );

                            _CswNbtResources.Nodes.makeNodeFromNodeTypeId(LatestVersionNT.NodeTypeId, delegate(CswNbtNode NewNode)
                            {
                                NewNode.copyPropertyValues(CswNbtNodeGenerator);
                                CswNbtPropertySetGeneratorTarget NewTarget = NewNode;
                                NewTarget.DueDate.DateTimeValue            = DueDate;
                                NewTarget.DueDate.setReadOnly(value: true, SaveToDb: true);       //bz # 5349
                                NewTarget.Generator.RelatedNodeId  = CswNbtNodeGenerator.NodeId;
                                NewTarget.Generator.CachedNodeName = CswNbtNodeGenerator.NodeName;
                                NewTarget.Parent.RelatedNodeId     = NewParentPk;
                                //NewTaskNodeAsTask.Completed.Checked = Tristate.False;

                                if (MarkFuture)
                                {
                                    NewTarget.IsFuture.Checked = CswEnumTristate.True;
                                }
                                else
                                {
                                    NewTarget.IsFuture.Checked = CswEnumTristate.False;
                                }

                                if (null != onBeforeInsertNode)
                                {
                                    onBeforeInsertNode(NewNode);
                                }
                                NodesCreated         += 1;
                                NewNode.PendingUpdate = true;
                                //NewNode.postChanges( true );
                            });
                        } // foreach( Int32 refNodeTypeId in SelectedNodeTypeIds )
                    }     //if ( null == ExistingNode )
                    else
                    {
                        if (false == MarkFuture)
                        {
                            if (ExistingNode.IsFuture.Checked == CswEnumTristate.True)
                            {
                                ExistingNode.IsFuture.Checked = CswEnumTristate.False;
                            }
                        }
                        else
                        {
                            if (DateTime.Now.Date >= ExistingNode.DueDate.DateTimeValue.Date)
                            {
                                ExistingNode.IsFuture.Checked = CswEnumTristate.False;
                            }
                        }
                        ExistingNode.postChanges(false);   //BZ # 6961
                    } //if-else ( null == ExistingNode )
                } // if( null != NewParentPk )
            } // foreach( CswPrimaryKey NewParentPk in Parents )

            // case 26111 - we're finished if we ran out of nodes to generate
            return(NodesCreated < GeneratorTargetLimit);
        } //makeNode()
        public void updateNextDueDateTest_NewGenerator()
        {
            CswNbtObjClassGenerator GeneratorNode = TestData.Nodes.createGeneratorNode(CswEnumRateIntervalType.WeeklyByDay);

            Assert.AreEqual(getNextDay(DayOfWeek.Monday), GeneratorNode.NextDueDate.DateTimeValue);
        }