public ConditionsPartEntry(Model model)
        {
            this.model = model;

            conditions tmpconditions = new conditions();
            conditions.Deserialize(model.conditionsPart.XML, out tmpconditions);
            conditions = tmpconditions;
        }
        public static bool doesConditionUseQuestion(XPathsPartEntry xppe, conditions conditions, condition c, string questionID)
        {
            //List<xpathsXpath> xpaths = ConditionsPartEntry.getXPathsUsedInCondition(c, xppe);
            List<xpathsXpath> xpaths = new List<xpathsXpath>();
            c.listXPaths(xpaths, conditions, xppe.getXPaths());

            foreach (xpathsXpath xpathObj in xpaths)
            {

                String xpathVal = xpathObj.dataBinding.xpath;

                if (xpathVal.StartsWith("/"))
                {
                    // simple
                    //System.out.println("question " + xpathObj.getQuestionID()
                    //        + " is in use via boolean condition " + conditionId);
                    if (xpathObj.questionID.Equals(questionID))
                    {
                        return true;
                    }

                }
                else if (xpathVal.Contains("position"))
                {
                    continue;
                }
                else
                {
                    //System.out.println(xpathVal);

                    String qid = xpathVal.Substring(
                        xpathVal.LastIndexOf("@id") + 5);
                    //						System.out.println("Got qid: " + qid);
                    qid = qid.Substring(0, qid.IndexOf("'"));
                    //						System.out.println("Got qid: " + qid);

                    //System.out.println("question " + qid
                    //        + " is in use via condition " + conditionId);

                    if (qid.Equals(questionID))
                    {
                        return true;
                    }

                }
            }
            return false;
        }
 public static bool LoadFromFile(string fileName, out conditions obj)
 {
     System.Exception exception = null;
     return LoadFromFile(fileName, out obj, out exception);
 }
 /// <summary>
 /// Deserializes xml markup from file into an conditions object
 /// </summary>
 /// <param name="fileName">string xml file to load and deserialize</param>
 /// <param name="obj">Output conditions object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
 public static bool LoadFromFile(string fileName, out conditions obj, out System.Exception exception)
 {
     exception = null;
     obj = default(conditions);
     try
     {
         obj = LoadFromFile(fileName);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }
 public static bool Deserialize(string xml, out conditions obj)
 {
     System.Exception exception = null;
     return Deserialize(xml, out obj, out exception);
 }
 /// <summary>
 /// Deserializes workflow markup into an conditions object
 /// </summary>
 /// <param name="xml">string workflow markup to deserialize</param>
 /// <param name="obj">Output conditions object</param>
 /// <param name="exception">output Exception value if deserialize failed</param>
 /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
 public static bool Deserialize(string xml, out conditions obj, out System.Exception exception)
 {
     exception = null;
     obj = default(conditions);
     try
     {
         obj = Deserialize(xml);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }
 public override void listXPaths(List<xpathsXpath> theList,
                 conditions conditions,
                 xpaths xPaths)
 {
     condition particle = conditions.getConditionByID(idField);
     particle.listXPaths(theList, conditions, xPaths);
 }
 public override void listXPaths(List<xpathsXpath> theList,
                 conditions conditions,
                 xpaths xPaths)
 {
     itemField.listXPaths(theList, conditions, xPaths);
 }
 public override void listXPaths(List<xpathsXpath> theList,
                 conditions conditions,
                 xpaths xPaths)
 {
     foreach (evaluable particle in itemsField)
     {
         particle.listXPaths(theList, conditions, xPaths);
     }
 }
 public override void listXPaths(List<xpathsXpath> theList,
                 conditions conditions,
                 xpaths xPaths)
 {
     xpathsXpath xpath = xPaths.getXPathByID(idField);
     theList.Add(xpath);
 }
 public abstract void listXPaths(List<xpathsXpath> theList,
         conditions conditions,
         xpaths xPaths);
        /// <summary>
        /// Create OpenDoPE parts, including optionally, question part.
        /// </summary>
        /// <param name="needQuestionsPart"></param>
        public void process(Microsoft.Office.Interop.Word.Document document, bool needQuestionsPart)
        {
            Model model = Model.ModelFactory(document);

            // Button shouldn't be available if this exists,
            // but ..
            if (model.conditionsPart == null)
            {
                conditions conditions = new conditions();
                string conditionsXml = conditions.Serialize();
                model.conditionsPart = addCustomXmlPart(document, conditionsXml);
            }

            if (model.componentsPart == null)
            {
                components components = new components();
                string componentsXml = components.Serialize();
                model.componentsPart = addCustomXmlPart(document, componentsXml);
            }

            // TODO 2013 12 if there is an existing questions part or answers part
            // I suspect we just want to keep it, rather than overwriting its contents!
            // Unless it is to do with supporting multiple Q, A parts?

            questionnaire q = null;
            if (model.questionsPart == null)
            {
                if (needQuestionsPart)
                {
                    q = new questionnaire();
                }
            }
            else
            {
                //needQuestionsPart = false;

                // Button shouldn't be available if this exists,
                // but ..
                q = new questionnaire();
                questionnaire.Deserialize(model.questionsPart.XML, out q);
            }

            // Add XPath
            xpaths xpaths = new xpaths();
            // Button shouldn't be available if this exists,
            // but ..
            if (model.xpathsPart != null)
            {
                xpaths.Deserialize(model.xpathsPart.XML, out xpaths);
            }

            if (needQuestionsPart)
            {
                // Are there content controls which need questions
                bool missingQuestions = false;
                foreach (Word.ContentControl cc in Globals.ThisAddIn.Application.ActiveDocument.ContentControls)
                {
                    if (cc.XMLMapping.IsMapped)
                    {
                        missingQuestions = true;
                        break;
                    }
                }
                if (missingQuestions)
                {
                    log.Warn("Document contains pre-existing bound content controls; without Questions.");
                    System.Windows.Forms.MessageBox.Show("This docx already contains bound content controls!");
                }
            }

            string xpathsXml = xpaths.Serialize();
            if (model.xpathsPart == null)
            {
                model.xpathsPart = addCustomXmlPart(document, xpathsXml);
            }
            else
            {
                CustomXmlUtilities.replaceXmlDoc(model.xpathsPart, xpathsXml);
            }

            if (model.questionsPart == null && needQuestionsPart)
            {
                string qxml = q.Serialize();
                model.questionsPart = addCustomXmlPart(document, qxml);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="target"></param>
        /// <param name="setSourceAttr">When saving a building block, we want to write the ID of the source part</param>
        /// <param name="setBindingStore">When re-using a building block, storeItemID to write to the xpath; otherwise null</param>
        /// <param name="overwriteExisting">If re-using a building block back into original source, we want to skip silently.
        /// When going the other way, we want to overwrite the logic in any existing building block (since
        /// it may have been updated).</param>
        public void injectLogic(Model targetModel, bool setSourceAttr, bool setBindingStore, bool overwriteExisting)
        {
            //Model targetModel = Model.ModelFactory(target);

            // XPaths
            XPathsPartEntry targetXppe = new XPathsPartEntry(targetModel);
            string sourceAttr = null;
            if (setSourceAttr)
            {
                sourceAttr = srcXPathsPart.Id;
            }
            string answersPartStoreID = null;
            if (setBindingStore)
            {
                answersPartStoreID = targetModel.answersPart.Id;
            }

            // .. add em
            foreach (xpathsXpath xp in BBxpaths)
            {
                xpathsXpath existing = targetXppe.getXPathByID(xp.id);
                if (existing == null)
                {
                    injectLogicXPath(targetXppe, xp, sourceAttr, answersPartStoreID);
                } else
                {
                    // Does it come from this doc?
                    //log.Debug("xp.source: " + xp.source);
                    //log.Debug("existing.source: " + existing.source);
                    //log.Debug("targetModel.xpathsPart.Id: " + targetModel.xpathsPart.Id);
                    if (xp.source != null && xp.source.Equals(targetModel.xpathsPart.Id))
                    {
                        // yes ..
                        if (overwriteExisting)
                        {
                            injectLogicXPath(targetXppe, xp, sourceAttr, answersPartStoreID);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (xp.source != null &&
                        existing.source != null &&
                        xp.source.Equals(existing.source))
                    {
                        // It has already been copied in.
                        // so don't do it again, whether we're copying to template
                        // (could go either way, but for now, only update from original source),
                        // or into docx
                        continue;

                    } else
                    {
                        // Yikes! ID collision
                        throw new BuildingBlockLogicException("XPath with ID " + xp.id + " is already present.");
                    }
                }
            }

            // Questions
            questionnaire targetQuestionnaire = new questionnaire();
            questionnaire.Deserialize(targetModel.questionsPart.XML, out targetQuestionnaire);
            // .. add em
            foreach (question q in BBquestions)
            {
                question existing = targetQuestionnaire.getQuestion(q.id);
                if (existing == null)
                {
                    targetQuestionnaire.questions.Add(q);
                    if (setSourceAttr)
                    {
                        q.source = srcQuestionsPart.Id;
                    }
                }
                else
                {
                    // Does it come from this doc?
                    //log.Debug("q.source: " + q.source);
                    //log.Debug("existing.source: " + existing.source);
                    //log.Debug("targetModel.questionsPart.Id: " + targetModel.questionsPart.Id);
                    if (q.source != null && q.source.Equals(targetModel.questionsPart.Id))
                    {
                        // yes ..
                        if (overwriteExisting)
                        {
                            targetQuestionnaire.questions.Add(q); // this is a HashSet, so we're overrwriting, not adding :-)
                            if (setSourceAttr)
                            {
                                q.source = targetModel.questionsPart.Id;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (q.source != null &&
                        existing.source != null &&
                        q.source.Equals(existing.source))
                    {
                        // It has already been copied in.
                        continue;
                    }
                    else
                    {
                        // Yikes! ID collision
                        throw new BuildingBlockLogicException("Question with ID " + q.id + " is already present.");
                    }
                }
            }

            // Answers
            answers targetAnswers = new answers();
            answers.Deserialize(targetModel.answersPart.XML, out targetAnswers);
            foreach (answer a in BBanswers)
            {
                answer existing = getAnswer(targetAnswers, a.id);
                if (existing == null)
                {
                    targetAnswers.Items.Add(a);
                    if (setSourceAttr)
                    {
                        a.source = srcAnswersPart.Id;
                    }
                }
                else
                {
                    // Does it come from this doc?
                    if (a.source != null && a.source.Equals(targetModel.answersPart.Id))
                    {
                        log.Debug("source is this part");
                        // yes ..
                        if (overwriteExisting)
                        {
                            log.Debug(".. and overwriting..");
                            targetAnswers.Items.Add(a); // this is a HashSet, so we're overrwriting, not adding :-)
                            if (setSourceAttr)
                            {
                                a.source = srcAnswersPart.Id;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (a.source != null &&
                        existing.source != null &&
                        a.source.Equals(existing.source))
                    {
                        // It has already been copied in.
                        log.Debug("this logic already present");
                        continue;
                    }
                    else
                    {
                        // Yikes! ID collision
                        throw new BuildingBlockLogicException("Answer with ID " + a.id + " from different source is already present.");
                    }
                }
            }
            foreach (repeat r in BBrepeats)
            {
                repeat existing = getRepeat(targetAnswers, r.qref);
                if (existing == null)
                {
                    targetAnswers.Items.Add(r);
                    if (setSourceAttr)
                    {
                        r.source = srcAnswersPart.Id;
                    }
                }
                else
                {
                    // Does it come from this doc?
                    if (r.source != null && r.source.Equals(targetModel.answersPart.Id))
                    {
                        // yes ..
                        if (overwriteExisting)
                        {
                            targetAnswers.Items.Add(r); // this is a HashSet, so we're overrwriting, not adding :-)
                            if (setSourceAttr)
                            {
                                r.source = srcAnswersPart.Id;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (r.source != null &&
                        existing.source != null &&
                        r.source.Equals(existing.source))
                    {
                        // It has already been copied in.
                        continue;
                    }
                    else
                    {
                        // Yikes! ID collision
                        throw new BuildingBlockLogicException("Answer with ID " + r.qref + " is already present.");
                    }
                }
            }

            // Conditions
            conditions targetConditions = new conditions();
            conditions.Deserialize(targetModel.conditionsPart.XML, out targetConditions);
            foreach (condition c in BBconditions)
            {
                condition existing = getCondition(targetConditions, c.id);
                if (existing == null)
                {
                    targetConditions.condition.Add(c);
                    if (setSourceAttr)
                    {
                        c.source = srcConditionsPart.Id;
                    }
                }
                else
                {
                    // Does it come from this doc?
                    if (c.source != null && c.source.Equals(targetModel.conditionsPart.Id))
                    {
                        // yes ..
                        if (overwriteExisting)
                        {
                            targetConditions.condition.Add(c);  // this is a HashSet, so we're overrwriting, not adding :-)
                            if (setSourceAttr)
                            {
                                c.source = targetModel.conditionsPart.Id;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (c.source != null &&
                        existing.source != null &&
                        c.source.Equals(existing.source))
                    {
                        // It has already been copied in.
                        continue;
                    }
                    else
                    {
                        // Yikes! ID collision
                        throw new BuildingBlockLogicException("Condition with ID " + c.id + " is already present.");
                    }
                }
            }

            // .. save: we only save if there have been no ID collisions.
            // Otherwise, we will have aborted with a BuildingBlockLogicException
            targetXppe.save();
            CustomXmlUtilities.replaceXmlDoc(targetModel.questionsPart, targetQuestionnaire.Serialize());
            CustomXmlUtilities.replaceXmlDoc(targetModel.conditionsPart, targetConditions.Serialize());
            CustomXmlUtilities.replaceXmlDoc(targetModel.answersPart, targetAnswers.Serialize());
        }
 public condition getCondition(conditions targetConditions, String id)
 {
     foreach (condition xx in targetConditions.condition)
     {
         if (xx.id.Equals(id))
         {
             return xx;
         }
     }
     return null;
 }
        public FormConditionBuilder(Word.ContentControl cc, ConditionsPartEntry cpe, condition existingCondition)
        {
            InitializeComponent();

            // NET 4 way; see http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.extensions.aspx
            FabDocxState fabDocxState = (FabDocxState)Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument).Tag;

            // NET 3.5 way, which requires using Microsoft.Office.Tools.Word.Extensions
            //FabDocxState fabDocxState = (FabDocxState)Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject(Globals.Factory).Tag;
            this.model = fabDocxState.model;
            xppe = new XPathsPartEntry(model);

            this.cc = cc;

            this.cpe = cpe;
            this.existingCondition = existingCondition;

            this.questionsPart = model.questionsPart;
            questionnaire qtmp = new questionnaire();
            questionnaire.Deserialize(questionsPart.XML, out qtmp);
            questionnaire = qtmp;

            conditions ctmp = new conditions();
            conditions.Deserialize(model.conditionsPart.XML, out ctmp);
            conditions = ctmp;

            log.Debug("conditions: " + conditions.Serialize());

            this.listBoxGovernor.Items.Add("all");
            this.listBoxGovernor.Items.Add("any");
            this.listBoxGovernor.Items.Add("none");
            this.listBoxGovernor.SelectedItem = "all";

            rowHelper = new Helpers.ConditionsFormRowHelper(model, xppe, questionnaire, cc, this);

            rowHelper.init(this.dataGridView);

            DataGridViewRow row = this.dataGridView.Rows[0];
            rowHelper.populateRow(row, null, null);
        }
        /// <summary>
        /// Create OpenDoPE parts, including optionally, question part.
        /// </summary>
        public void process()
        {
            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                document = Globals.ThisAddIn.Application.ActiveDocument;
            }
            catch (Exception ex)
            {
                Mbox.ShowSimpleMsgBoxError("No document is open/active. Create or open a docx first.");
                return;
            }

            Model model = Model.ModelFactory(document);

            // Button shouldn't be available if this exists,
            // but ..
            if (model.conditionsPart == null)
            {
                conditions conditions = new conditions();
                string conditionsXml = conditions.Serialize();
                model.conditionsPart = addCustomXmlPart(document, conditionsXml);
            }

            if (model.componentsPart == null)
            {
                components components = new components();
                string componentsXml = components.Serialize();
                model.componentsPart = addCustomXmlPart(document, componentsXml);
            }

            // Add XPath
            xpaths xpaths = new xpaths();
            // Button shouldn't be available if this exists,
            // but ..
            if (model.xpathsPart != null)
            {
                xpaths.Deserialize(model.xpathsPart.XML, out xpaths);
            }
            int idInt = 1;
            foreach (Word.ContentControl cc in Globals.ThisAddIn.Application.ActiveDocument.ContentControls)
            {
                if (cc.XMLMapping.IsMapped)
                {
                    log.Debug("Adding xpath for " + cc.ID);
                    // then we need to add an XPath
                    string xmXpath = cc.XMLMapping.XPath;

                    xpathsXpath item = new xpathsXpath();
                    // I make no effort here to check whether the xpath
                    // already exists, since the part shouldn't already exist!

                    item.id = "x" + idInt;

                    xpathsXpathDataBinding db = new xpathsXpathDataBinding();
                    db.xpath = xmXpath;
                    db.storeItemID = cc.XMLMapping.CustomXMLPart.Id;
                    if (!string.IsNullOrWhiteSpace(cc.XMLMapping.PrefixMappings))
                        db.prefixMappings = cc.XMLMapping.PrefixMappings;
                    item.dataBinding = db;

                    xpaths.xpath.Add(item);

                    // Write tag
                    TagData td = new TagData(cc.Tag);
                    td.set("od:xpath", item.id);
                    cc.Tag = td.asQueryString();

                    log.Debug(".. added for " + cc.ID);
                    idInt++;
                }
            }
            string xpathsXml = xpaths.Serialize();
            if (model.xpathsPart == null)
            {
                model.xpathsPart = addCustomXmlPart(document, xpathsXml);
            }
            else
            {
                CustomXmlUtilities.replaceXmlDoc(model.xpathsPart, xpathsXml);
            }

            Microsoft.Office.Tools.Word.Document extendedDocument
                = Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject(Globals.Factory);

            //Microsoft.Office.Tools.CustomTaskPane ctp
            //    = Globals.ThisAddIn.createCTP(document, cxp, xpathsPart, conditionsPart, questionsPart, componentsPart);
            //extendedDocument.Tag = ctp;
            //// Want a 2 way association
            //WedTaskPane wedTaskPane = (WedTaskPane)ctp.Control;
            //wedTaskPane.associatedDocument = document;

            //extendedDocument.Shutdown += new EventHandler(
            //    Globals.ThisAddIn.extendedDocument_Shutdown);

            //taskPane.setupCcEvents(document);

            log.Debug("Done. Task pane now also open.");
        }