public static Guid Copy(Template template, Guid copyFromID, string newName)
		{
			Workflow workflowFrom = template.FindWorkflow(copyFromID);
			if (workflowFrom == null)
				return Guid.Empty;

            Workflow workflowCopy = workflowFrom.Copy(newName, template is ManagedItem, template);
			workflowCopy.Name = newName;
			template.Workflows.Add(workflowCopy);
			return workflowCopy.ID;
		}
        public static ManagedItemStore CreateStore(
            ILog log, 
            Template template, 
            string managedItem, 
            Guid managedItemID, 
            string managedItemNumber, 
            string status, 
            string state, 
            Dictionary<string /* Term Name */, Term> templateTerms,
            List<string> basicTermsToProcess,
            List<string> complexListTermsToProcess)
        {
            ManagedItemStore managedItemStore = new ManagedItemStore(template.ID, template.Name, managedItemNumber, status, state);
            bool basicTermsProcessed = false;
            bool complexListsProcessed = false;
            bool workflowsProcessed = false;
            int basicTermsToProcessCount = basicTermsToProcess.Count;
            int complexListTermsToProcessCount = complexListTermsToProcess.Count;

            XmlTextReader txtReader = new XmlTextReader(new MemoryStream(Encoding.UTF8.GetBytes(managedItem)));
            XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.IgnoreWhitespace = true;
            readerSettings.IgnoreComments = true;
            readerSettings.IgnoreProcessingInstructions = true;

            using (XmlReader reader = XmlReader.Create(txtReader, readerSettings))
            {
                reader.MoveToContent();     //Reader initialized and positioned at the 'TemplateDef' node.

                while (!(basicTermsProcessed && complexListsProcessed && workflowsProcessed))
                {
                    if (!reader.Read())     //At start element of a TemplateDef child node.
                    {
                        throw new Exception(string.Format("CreateStore call stopped unexpectedly on failed reader.Read call for ManagedItemNumber '{0}', ManagedItemID '{1}'", managedItemNumber, managedItemID.ToString()));
                    }

                    //Ensure completion before quitting...
                    if (reader.NodeType == XmlNodeType.EndElement && reader.Name == XMLNames._E_TemplateDef)
                    {
                        string error = string.Empty;
                        if (basicTermsToProcessCount > 0 && !basicTermsProcessed)
                            error += "Basic Terms were not processed.  ";
                        if (complexListTermsToProcessCount > 0 && !complexListsProcessed)
                            error += "Complex Lists were not processed.  ";
                        if (!workflowsProcessed)
                            error += "WorkFlows were not processed.  ";
                        if (!string.IsNullOrEmpty(error))
                            throw new Exception(string.Format("CreateStore call at end element before processing completed for ManagedItemNumber '{0}', ManagedItemID '{1}'.  {2}", managedItemNumber, managedItemID.ToString(), error));
                        break;
                    }

                    switch (reader.Name)
                    {
                        case XMLNames._E_Workflows:
                            Guid activeWorkflowID = new Guid(reader.GetAttribute(XMLNames._A_ActiveWorkflowID));
                            //NOTE - The search for active workflow may fail if the template information does not match the manageditem's information.
                            Workflow activeWorkflow = template.FindWorkflow(activeWorkflowID);
                            if (activeWorkflow != null)
                                managedItemStore.ActiveWorkflow = activeWorkflow.Name;
                            else
                                managedItemStore.ActiveWorkflow = string.Empty;
                            using (reader.ReadSubtree()) { }
                            workflowsProcessed = true;
                            break;

                        case XMLNames._E_Terms:
                            if (basicTermsToProcess.Count > 0)
                            {
                                ReadTermStore(
                                    template.Name,
                                    managedItemID,
                                    reader.ReadSubtree(),
                                    managedItemStore,
                                    templateTerms,
                                    basicTermsToProcess);
                            }
                            else
                            {
                                using (reader.ReadSubtree()) { }
                            }
                            basicTermsProcessed = true;
                            break;

                        case XMLNames._E_ComplexLists:
                            if (complexListTermsToProcess.Count > 0)
                            {
                                ReadComplexListStore(
                                    reader.ReadSubtree(),
                                    managedItemStore,
                                    templateTerms,
                                    complexListTermsToProcess);
                            }
                            else
                            {
                                using (reader.ReadSubtree()) { }
                            }
                            complexListsProcessed = true;
                            break;

                        case XMLNames._E_Events:
                        case XMLNames._E_Document:
                        case XMLNames._E_Comments:
                        case XMLNames._E_TermDependencies:
                        case XMLNames._E_DetailedDescriptions:
                        case XMLNames._E_TermGroups:
                        case XMLNames._E_DocumentPrinters:
                        default:
                            using (reader.ReadSubtree()) { }
                            break;
                    }

                }
            }

            return managedItemStore;
        }
		public static Workflow ActiveWorkflow(Template template)
		{
			if (template != null)
			{
				return template.FindWorkflow(template.ActiveWorkflowID);
			}
			return null;
		}