コード例 #1
0
        public QuestionListHelper(Model model, XPathsPartEntry xppe, questionnaire questionnaire, 
                    Word.ContentControl cc)
        {
            this.model = model;
            this.xppe = xppe;
            this.questionnaire = questionnaire;

            this.cc = cc;
        }
コード例 #2
0
        public void updateQuestionsPart(question q)
        {
            questionnaire questionnaire = new questionnaire();
            questionnaire.Deserialize(questionsPart.XML, out questionnaire);

            questionnaire.questions.Add(q);

            // Save it in docx
            string result = questionnaire.Serialize();
            log.Info(result);
            CustomXmlUtilities.replaceXmlDoc(questionsPart, result);
        }
コード例 #3
0
        public FormMoveQuestion(Office.CustomXMLPart answersPart,
            List<Word.ContentControl> relevantRepeats,
            questionnaire questionnaire, 
            string questionID,
            XPathsPartEntry xppe)
        {
            InitializeComponent();

            this.textBoxQuestion.Text = questionnaire.getQuestion(questionID).text;

            this.controlQuestionVaryWhichRepeat1.init(answersPart,
                relevantRepeats,
                questionnaire, questionID,
                xppe);
        }
コード例 #4
0
        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>
        // RULE: A variable cc can copied wherever.
        // If its repeat ancestors change, its "vary in repeat"
        // will need to change (to lowest common denominator).
        // If this cc is not in any repeat,
        // make the answer top level and we're done
        // Otherwise, could assume its position is OK wrt
        // existing repeats.
        // So if anything, we just need to move it
        // up the tree until
        // we reach a node which contains this additional repeat.
        // But we'd like this code to
        // be used for both moves and copy. (Move case
        // needs this constraint relaxed)
        // So our algorithm finds viable positions
        // (ie ancestors common to all
        // repeats).
        // That node and higher are candidates for new position
        // Ask user to choose.
        /// </summary>
        protected void handleXPath(string xpathID, bool dontAskIfOkAsis)
        {
            xpathsXpath xp = xppe.getXPathByID(xpathID);

            string questionID = xp.questionID;

            // If this cc is not in any repeat,
            // make the answer top level and we're done

            // Otherwise, we can assume its position is OK wrt
            // existing repeats.

            // So if anything, we just need to move it
            // up the tree until
            // we reach a node which contains this additional repeat.

            // But for the variable move case (handled in MoveHandler)
            // an existing repeat will no longer be a constraint.
            // So better to just have a single algorithm which
            // finds viable positions (ie ancestors common to all
            // repeats).

            // 2 algorithms for doing this.
            // The first:  Make a list of the ancestors of the
            // first repeat.  Then cross off that list anything
            // which isn't an ancestor of the other repeats.

            // The second: Get XPath for each repeat.
            // Find the shortest XPath.
            // Then find the shortest substring common to each
            // repeat. Then make a list out of that.

            // I like the first better.

            // Find all cc's in which this question is used.
            QuestionHelper qh = new QuestionHelper(xppe, cpe);
            List<Word.ContentControl> thisQuestionControls = qh.getControlsUsingQuestion(questionID, xpathID);

            // For each such cc, find closest repeat ancestor cc (if any).
            // With each such repeat, do the above algorithm.

            // Find all cc's in which this question is used.
            List<Word.ContentControl> relevantRepeats = new List<Word.ContentControl>();
            foreach (Word.ContentControl ccx in thisQuestionControls)
            {
                Word.ContentControl rpt = RepeatHelper.getYoungestRepeatAncestor(ccx);
                if (rpt == null)
                {
                    // make the answer top level and we're done.
                    // That means moving it in AF, and XPaths part.
                    log.Info("question " + questionID + " used at top level, so moving it there.");
                    NodeMover nm = new NodeMover();
                    nm.Move(xp.dataBinding.xpath, "/oda:answers");
                    nm.adjustBinding(thisQuestionControls, "/oda:answers", questionID);
                    return;

                }
                else
                {
                    relevantRepeats.Add(rpt);
                }
            }

            Office.CustomXMLPart answersPart = model.answersPart; // userParts[0]; // TODO: make this better

            // That node and higher are candidates for new position
            // Ask user to choose, or cancel (and remove this cc,
            // but what about any impending child add events??  Maybe
            // need a cancel state)

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

            FormMoveQuestion formMoveQuestion = new FormMoveQuestion(answersPart, relevantRepeats, questionnaire, questionID, xppe);
            if (dontAskIfOkAsis
                && formMoveQuestion.OkAsis() )
            {
                // Do nothing
            }
            else
            {
                formMoveQuestion.ShowDialog();
                formMoveQuestion.moveIfNecessary(questionID, xp, answersPart);

                //string varyInRepeat = formMoveQuestion.getVaryingRepeat();
                //if (varyInRepeat == null)
                //{
                //    // make the answer top level and we're done.
                //    NodeMover nm = new NodeMover();
                //    nm.Move(xp.dataBinding.xpath, "/oda:answers");
                //    nm.adjustBinding(thisQuestionControls, "/oda:answers", questionID);
                //}
                //else
                //{
                //    // Move it to the selected repeat
                //    // get the node corresponding to the repeat's row
                //    Office.CustomXMLNode node = answersPart.SelectSingleNode("//oda:repeat[@qref='" + varyInRepeat + "']/oda:row[1]");
                //    if (node == null)
                //    {
                //        log.Error("no node for nested repeat " + varyInRepeat);
                //    }
                //    string toRepeat = NodeToXPath.getXPath(node);
                //    NodeMover nm = new NodeMover();
                //    nm.Move(xp.dataBinding.xpath, toRepeat);
                //    nm.adjustBinding(thisQuestionControls, toRepeat, questionID);

                //}
            }
            formMoveQuestion.Dispose();
        }
コード例 #6
0
        /// <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);
            }
        }
コード例 #7
0
        public FormRepeat(Office.CustomXMLPart questionsPart,
            Office.CustomXMLPart answersPart,
            Model model,
            Word.ContentControl cc)
        {
            InitializeComponent();

            this.model = model;
            this.cc = cc;

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

            this.answersPart = answersPart;

            Office.CustomXMLNodes answers = answersPart.SelectNodes("//oda:repeat");
            foreach (Office.CustomXMLNode answer in answers)
            {
                this.answerID.Add(CustomXMLNodeHelper.getAttribute(answer, "qref")); // ID
            }

            // Suggest ID .. the idea is that
            // the question ID = answer ID.
               // this.ID = generateId();

            xppe = new XPathsPartEntry(model);
            // List of repeat names, for re-use purposes
            // (we need a map from repeat name (shown in the UI) to repeat id,
            //  which is xppe.xpathId).
            // What is it that distinguishes a repeat from any other question?
            // Answer: The fact that the XPath pointing to it ends with oda:row

            // Only show repeats which are in scope (since this repeat is not allowed elsewhere)
            // - if no ancestor repeat, then top level repeats.
            // - if there is an ancestor repeat, then just those which are direct children of it.
            Word.ContentControl rptAncestor = RepeatHelper.getYoungestRepeatAncestor(cc);
            String scope = "/oda:answers";
            if (rptAncestor != null)
            {
                // find its XPath
                scope = xppe.getXPathByID(  (new TagData(rptAncestor.Tag)).getRepeatID() ).dataBinding.xpath;

            }

            repeatNameToIdMap = new Dictionary<string, string>();
            foreach (xpathsXpath xpath in xppe.getXPaths().xpath)
            {
                if (xpath.dataBinding.xpath.EndsWith("oda:row"))
                {
                    if (isRepeatInScope(scope, xpath.dataBinding.xpath)) {
                        // the repeat "name" is its question text.
                        // Get that.
                        question q = questionnaire.getQuestion(xpath.questionID);
                        repeatNameToIdMap.Add(q.text, xpath.id);
                    }
                }
            }
            // Populate the comboBox
            foreach(KeyValuePair<String,String> entry in repeatNameToIdMap)
            {
                this.comboBoxRepeatNames.Items.Add(entry.Key);
            }
        }
コード例 #8
0
        public void buttonRepeat_Click(Office.IRibbonControl control)
        {
            Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;

            FabDocxState fabDocxState = (FabDocxState)Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject(Globals.Factory).Tag;

            Model model = fabDocxState.model;
            Office.CustomXMLPart answersPart = model.answersPart; //.userParts[0]; // TODO: make this better
            XPathsPartEntry xppe = new XPathsPartEntry(model); // used to get entries

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

            Microsoft.Office.Interop.Word.Range rng = document.ActiveWindow.Selection.Range;

            // Are there any content controls in the selection?
            Word.ContentControls ccs = rng.ContentControls;

            // First identify nested repeats
            List<Word.ContentControl> nestedRepeats = new List<Word.ContentControl>();
            foreach (Word.ContentControl desc in ccs)
            {
                if (desc.Tag.Contains("od:repeat"))
                {
                    nestedRepeats.Add(desc);
                }
            }

            // questions contains questions wrapped by repeat,
            // but not nested inside another repeat
            List<question> questions = new List<question>();

            foreach (Word.ContentControl desc in ccs)
            {
                if (desc.Tag.Contains("od:repeat"))
                {
                    continue; // will handle repeats later
                }

                // exclude if in nested repeat, since
                // this question will have previously been dealt with
                // (ie it already varies with that repeat, or the
                // use has said they don't want it to)
                if (isInside(nestedRepeats, desc))
                {
                    continue;
                }

                //log.Warn("got a desc with tag " + desc.Tag);
                // Get the tag
                if (desc.Tag.Contains("od:xpath"))
                {
                    TagData td = new TagData(desc.Tag);
                    string xpathID = td.getXPathID();
                    //log.Warn("xpath is  " + xpathID);
                    xpathsXpath xp = xppe.getXPathByID(xpathID);
                    log.Warn("qid is  " + xp.questionID);
                    question q = questionnaire.getQuestion(xp.questionID);
                    if (q == null)
                    {
                        log.Error("Consistency issue: couldn't find question {0} used in xpath {1}", xp.questionID, xpathID);
                    }
                    else
                    {
                        questions.Add(q);
                    }
                }
                else if (desc.Tag.Contains("od:condition"))
                {
                    // TODO: find questions inside conditions
                }
            }

            if (questions.Count > 0)
            {
                // Rule: Only questions which aren't used elsewhere can vary in a repeat.
                // Check that none of the questions that will be
                // inside the repeat are also used outside of it.
                List<question> questionsUsedOutside = new List<question>();
                foreach (Word.ContentControl ccx in document.ContentControls)
                {
                    if (isListed(ccs, ccx))
                    {
                        // this control is inside the repeat
                    }
                    else
                    {
                        // its outside, so look at its question
                        // TODO: conditions, repeats
                        if (ccx.Tag.Contains("od:xpath"))
                        {
                            TagData td = new TagData(ccx.Tag);
                            string xpathID = td.getXPathID();
                            //log.Warn("xpath is  " + xpathID);
                            xpathsXpath xp = xppe.getXPathByID(xpathID);
                            //log.Warn("qid is  " + xp.questionID);
                            question q = questionnaire.getQuestion(xp.questionID);
                            if (q == null)
                            {
                                log.Error("Consistency issue: couldn't find question {0} used in xpath {1}", xp.questionID, xpathID);
                            }
                            else
                            {
                                if (questions.Contains(q))
                                {
                                    questionsUsedOutside.Add(q);
                                }
                            }
                        }
                    }
                } // foreach

                // If they are, they can't vary in repeat.  Get the user to OK this.
                if (questionsUsedOutside.Count == 0)
                {
                    log.Info("None of the questions in wrapping repeat are used elsewhere");
                }
                else
                {
                    log.Info(questionsUsedOutside.Count + " of the questions in wrapping repeat are used elsewhere");
                    DialogResult dresult = MessageBox.Show(
                        questionsUsedOutside.Count + " of the questions here are also used elsewhere. If you continue, these won't vary in each repeat.",
                        "Questions used elsewhere", MessageBoxButtons.OKCancel);
                    if (dresult == DialogResult.OK)
                    {
                        // Just remove them from the list
                        foreach (question qx in questionsUsedOutside)
                        {
                            questions.Remove(qx);
                        }
                    }
                    else
                    {
                        log.Info("User cancelled wrapping repeat coz questions used elsewhere");
                        return;
                    }
                }
            }

            // Create control
            Word.ContentControl wrappingRepeatCC = null;
            object oRng = rng;
            try
            {
                fabDocxState.inPlutextAdd = true;
                wrappingRepeatCC = document.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref oRng);
                //cc.MultiLine = true; // Causes error for RichText
            }
            catch (System.Exception ex)
            {
                log.Warn(ex);
                MessageBox.Show("Selection must be either part of a single paragraph, or one or more whole paragraphs");
                fabDocxState.inPlutextAdd = false;
                return;
            }

            FormRepeat formRepeat = new FormRepeat(model.questionsPart, answersPart, model, wrappingRepeatCC);
            formRepeat.ShowDialog();
            string repeatId = formRepeat.ID;
            formRepeat.Dispose();

            // necessary here? shouldn't be..
            //answersPart.NamespaceManager.AddNamespace("answers", "http://opendope.org/answers");

            // Destination for moves
            Office.CustomXMLNode destination = answersPart.SelectSingleNode("//oda:repeat[@qref='" + repeatId + "']/oda:row");
            if (destination == null)
            {
                log.Error("no rpt node " + repeatId);
            }

            Dictionary<string, string> xpathChanges = new Dictionary<string, string>();

            // Questions, Conditions
            // ^^^^^^^^^^^^^^^^^^^^^
            // If so, the associated questions may need to be moved into the repeat in the answers XML.
            // Present a table of questions, where the user can say yes/no to each,
            // then move.. table excludes:
            // 1. any that are used outside the repeat, since these can't be made to vary (see above)
            // 2. any that are in a nested repeat
            if (questions.Count > 0)
            {
                FormRepeatWhichVariables formRepeatWhichVariables = new FormRepeatWhichVariables(questions);
                formRepeatWhichVariables.ShowDialog();

                List<question> questionsWhichRepeat = formRepeatWhichVariables.getVars();

                formRepeatWhichVariables.Dispose();

                log.Info(answersPart.XML);

                foreach (question q in questionsWhichRepeat)
                {
                    // Find the relevant answer (by ID)
                    // (easiest to do using XPath on XML document
                    Office.CustomXMLNode node = answersPart.SelectSingleNode("//oda:answer[@id='" + q.id + "']");
                    if (node == null)
                    {
                        log.Error("no node " + q.id);
                    }
                    string fromXPath = NodeToXPath.getXPath(node);
                    log.Info("from: " + fromXPath);

                    // Move it
                    String nodeXML = node.XML; // No API to add a node!
                    node.ParentNode.RemoveChild(node);
                    destination.AppendChildSubtree(nodeXML);

                    // So we'll have to change its xpath in XPaths part
                    // eg from:
                    //   "/oda:answers/oda:answer[@id='qa_2']"
                    // to:
                    //   "/oda:answers/oda:repeat[@qref='rpt1"]/oda:row[1]/oda:answer[@id='qa_2']"
                    //
                    // CustomXMLNode's Xpath produces something like: /ns2:answers[1]/ns2:answer[1]
                    // which we don't want

                    string toXPath = NodeToXPath.getXPath(destination.LastChild);
                    log.Info("to: " + toXPath);

                    xpathChanges.Add(fromXPath, toXPath);
                }

            }
            // nested repeats
            // ^^^^^^^^^^^^^^
            // 1. move the nested repeat answer structure
            // 2. change the xpath for all questions re anything in the nested repeat

            // Note: if wrapping repeat r0 around r1 which in turn contains r2,
            // must avoid explicitly processing  r2, since doing stuff to r1 here is
            // enough to take care of r2.

            // So, step 0. Find top level nested repeats
            // Already have nestedRepeats list, so just remove from it
            // those which aren't top level.
            foreach (Word.ContentControl desc in nestedRepeats)
            {
                if (!desc.ParentContentControl.ID.Equals(wrappingRepeatCC.ID) )
                {
                    // not top level, so remove
                    nestedRepeats.Remove(desc);
                }
            }

            if (nestedRepeats.Count > 0)
            {
                foreach (Word.ContentControl desc in nestedRepeats)
                {
                    TagData td = new TagData(desc.Tag);
                    string nestedRepeatXPathID = td.getRepeatID();

                    // Get the XPath, to find the question ID,
                    // which is what we use to find the repeat answer.
                    xpathsXpath xp = xppe.getXPathByID(nestedRepeatXPathID);

                    // 1. move the nested repeat answer structure
                    Office.CustomXMLNode node = answersPart.SelectSingleNode("//oda:repeat[@qref='" + xp.questionID + "']");
                    if (node == null)
                    {
                        log.Error("no node for nested repeat " + xp.questionID);
                    }
                    string fromXPath = NodeToXPath.getXPath(node);
                    log.Info("from: " + fromXPath);

                    // Move it
                    String nodeXML = node.XML; // No API to add a node!
                    node.ParentNode.RemoveChild(node);
                    destination.AppendChildSubtree(nodeXML);

                    // 2. change the xpath for all questions re anything in the nested repeat
                    // With a bit of luck, this will just work!
                    string toXPath = NodeToXPath.getXPath(destination.LastChild);
                    log.Info("to: " + toXPath);

                    xpathChanges.Add(fromXPath, toXPath);
                }
            }

            // Now do the substitutions in the XPaths part - for all
            string xpaths = model.xpathsPart.XML;
            foreach (KeyValuePair<string, string> entry in xpathChanges)
            {
                xpaths = xpaths.Replace(entry.Key, entry.Value);
            }
            CustomXmlUtilities.replaceXmlDoc(model.xpathsPart, xpaths);
            //log.Info(model.xpathsPart.XML);
            //log.Info(answersPart.XML);

            // Now do the substitutions in the content control databindings
            // (Added 2012 12 16, since docx4j relies on the databinding element to do its bit)
            foreach (Word.ContentControl cc in wrappingRepeatCC.Range.ContentControls)
            //foreach (Word.ContentControl cc in Globals.ThisAddIn.Application.ActiveDocument.ContentControls)
            {
                // XMLMapping.IsMapped returns false here,
                // in cases where it is mapped! So avoid using that.
                // (Could be a defect elsewhere .. check for usage)
                if (cc.XMLMapping!=null
                    && cc.XMLMapping.XPath!=null
                    && cc.XMLMapping.PrefixMappings!=null)
                {
                    foreach (KeyValuePair<string, string> entry in xpathChanges)
                    {
                        //log.Info("Comparing " + cc.XMLMapping.XPath + " with " + entry.Key);

                        if (cc.XMLMapping.XPath.Equals(entry.Key))
                        {
                            // matched, so replace
                            cc.XMLMapping.SetMapping(entry.Value, cc.XMLMapping.PrefixMappings, answersPart);
                            break;
                        }
                    }
                }
            }
        }
 public QuestionListHelperForConditionsForm(Model model, XPathsPartEntry xppe, questionnaire questionnaire,
             Word.ContentControl cc)
     : base(model,  xppe,  questionnaire, cc)
 {
 }
コード例 #10
0
        public LibraryHelper(Model srcModel)
        {
            srcXppe = new XPathsPartEntry(srcModel); // used to get entries
            this.srcXPathsPart = srcModel.xpathsPart;

            srcCpe = new ConditionsPartEntry(srcModel);
            this.srcConditionsPart = srcModel.conditionsPart;

            this.srcQuestionsPart = srcModel.questionsPart;
            srcQuestionnaire = new questionnaire();
            questionnaire.Deserialize(srcQuestionsPart.XML, out srcQuestionnaire);

            srcAnswersPart = srcModel.answersPart;
            srcAnswers = new answers();
            answers.Deserialize(srcAnswersPart.XML, out srcAnswers);
        }
コード例 #11
0
        /*
         * For each question, I need to keep track of the last value
         * it had on a given row.  This I can store in DataGridViewBand.Tag
         * so a Dictionary<Q, object> is OK for this.
          *
          * Actually, the last value seems to be retained. Without looking
          * to see why/how, I don't need to do anything.
         *
         */
        public ConditionsFormRowHelper(Model model, XPathsPartEntry xppe, questionnaire questionnaire, 
                    Word.ContentControl cc, Forms.FormConditionBuilder fcb)
        {
            this.model = model;
            this.xppe = xppe;
            this.questionnaire = questionnaire;
            this.fcb = fcb;

            this.cc = cc;
        }
コード例 #12
0
 /// <summary>
 /// Deserializes xml markup from file into an questionnaire object
 /// </summary>
 /// <param name="fileName">string xml file to load and deserialize</param>
 /// <param name="obj">Output questionnaire 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 questionnaire obj, out System.Exception exception)
 {
     exception = null;
     obj = default(questionnaire);
     try
     {
         obj = LoadFromFile(fileName);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }
コード例 #13
0
 public static bool Deserialize(string xml, out questionnaire obj)
 {
     System.Exception exception = null;
     return Deserialize(xml, out obj, out exception);
 }
コード例 #14
0
 /// <summary>
 /// Deserializes workflow markup into an questionnaire object
 /// </summary>
 /// <param name="xml">string workflow markup to deserialize</param>
 /// <param name="obj">Output questionnaire 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 questionnaire obj, out System.Exception exception)
 {
     exception = null;
     obj = default(questionnaire);
     try
     {
         obj = Deserialize(xml);
         return true;
     }
     catch (System.Exception ex)
     {
         exception = ex;
         return false;
     }
 }
        public void init(
            Office.CustomXMLPart answersPart,
            List<Word.ContentControl> relevantRepeats,
            questionnaire questionnaire, 
            string questionID,
            XPathsPartEntry xppe)
        {
            List<Office.CustomXMLNode> commonAncestors =null;
            foreach (Word.ContentControl repeat in relevantRepeats)
            {
                log.Info("considering relevantRepeat cc " + repeat.ID + " " + repeat.Tag);
                TagData repeatTD = new TagData(repeat.Tag);
                string repeatXPathID = repeatTD.getRepeatID();
                xpathsXpath repeatXP = xppe.getXPathByID(repeatXPathID);
                Office.CustomXMLNode repeatNode = answersPart.SelectSingleNode(repeatXP.dataBinding.xpath).ParentNode;

                if (commonAncestors == null)
                {
                    // First entry, so init
                    // Make a list of the ancestors of the
                    // first repeat.
                    commonAncestors = new List<Microsoft.Office.Core.CustomXMLNode>();
                    commonAncestors.Add(repeatNode);
                    log.Info("Added to common ancestors " + CustomXMLNodeHelper.getAttribute(repeatNode, "qref"));
                    addAncestors(commonAncestors, repeatNode);
                }
                else
                {
                    // cross off that list anything
                    // which isn't an ancestor of the other repeats.
                    List<Microsoft.Office.Core.CustomXMLNode> whitelist = new List<Microsoft.Office.Core.CustomXMLNode>();
                    whitelist.Add(repeatNode);
                    addAncestors(whitelist, repeatNode);
                    removeNonCommonAncestor(commonAncestors, whitelist);
                }

                if (commonAncestors.Count == 0) break;
            }
            if (commonAncestors == null)
            {
                    commonAncestors = new List<Microsoft.Office.Core.CustomXMLNode>();
            }

            // Is it OK where it is?
            // Yes - if it is top level
            log.Debug(questionID + " --> " + xppe.getXPathByQuestionID(questionID).dataBinding.xpath);
            // eg /oda:answers/oda:repeat[@qref='rpt1']/oda:row[1]/oda:answer[@id='qa_2']
            OkAsis = (xppe.getXPathByQuestionID(questionID).dataBinding.xpath.IndexOf("oda:repeat") < 0);
            Microsoft.Office.Core.CustomXMLNode currentPos = null; // so we can highlight existing choice
            // Yes - if it is a child of common ancestors
            if (OkAsis)
            {
                log.Debug("its top level");
            }
            else
            {
                foreach (Microsoft.Office.Core.CustomXMLNode currentNode in commonAncestors)
                {
                    Microsoft.Office.Core.CustomXMLNode selection =
                        currentNode.SelectSingleNode("oda:row[1]/oda:answer[@id='" + questionID + "']");
                    if (selection != null)
                    {
                        log.Debug("found it");
                        OkAsis = true;
                        currentPos = currentNode;
                        break;
                    }
                }
            }

            // Now make the tree from what is left in commonAncestors

            root = new TreeNode("Ask only once");
            this.treeViewRepeat.Nodes.Add(root);

            TreeNode thisNode = null;
            TreeNode previousNode = null;
            treeViewRepeat.HideSelection = false; // keep selection when focus is lost

            TreeNode nodeToSelect = null;

            foreach (Microsoft.Office.Core.CustomXMLNode currentNode in commonAncestors)
            {
                // Find the question associated with this repeat
                string rptQRef = CustomXMLNodeHelper.getAttribute(currentNode, "qref");
                    //currentNode.Attributes[1].NodeValue;
                question q = questionnaire.getQuestion(rptQRef);
                if (q == null)
                {
                    log.Error("no question with id " + rptQRef);
                }
                thisNode = new TreeNode(q.text);
                thisNode.Tag = rptQRef;

                if (currentNode == currentPos)
                {
                    nodeToSelect = thisNode;
                }

                if (previousNode == null)
                {
                    // Check the innermost (may be overridden below by what level user already had, if possible)
                    this.treeViewRepeat.SelectedNode = thisNode;
                }
                else
                {
                    thisNode.Nodes.Add(previousNode);
                }
                previousNode = thisNode;

            }

            if (thisNode != null)
            {
                root.Nodes.Add(thisNode);
            }
            treeViewRepeat.ExpandAll();

            if (nodeToSelect != null)
            {
                this.treeViewRepeat.SelectedNode = nodeToSelect;
                originalValue = thisNode;
            }
            else if (OkAsis)
            {
                originalValue = root;
                this.treeViewRepeat.SelectedNode = root;
            }
        }
        public void init(
            Office.CustomXMLPart answersPart,
            questionnaire questionnaire,
            question q,
            XPathsPartEntry xppe,
            ConditionsPartEntry cpe)
        {
            QuestionHelper qh = new QuestionHelper(xppe, cpe);
            thisQuestionControls = qh.getControlsUsingQuestion(q);

            List<Word.ContentControl> relevantRepeats = new List<Word.ContentControl>();
            foreach (Word.ContentControl ccx in thisQuestionControls)
            {
                Word.ContentControl rpt = RepeatHelper.getYoungestRepeatAncestor(ccx);
                if (rpt == null)
                {
                    // will have to make the answer top level and we're done.
                    break;
                }
                else
                {
                    relevantRepeats.Add(rpt);
                }
            }

            init(
                answersPart,
                relevantRepeats,
                 questionnaire,
                 q.id,
                 xppe);
        }
        /// <summary>
        /// When question is first being added, it is being added to a particular CC,
        /// so we only need to consider that CC's ancestors.
        /// 
        /// When a question is being edited, it could be in several content controls,
        /// so that case is handled by the more generic method.
        /// </summary>
        /// <param name="cc"></param>
        /// <param name="questionnaire"></param>
        /// <param name="xppe"></param>
        public void init(Word.ContentControl cc, questionnaire questionnaire, XPathsPartEntry xppe)
        {
            root = new TreeNode("Ask only once");

            Word.ContentControl currentCC = cc.ParentContentControl;

            TreeNode thisNode = null;
            TreeNode previousNode = null;
            treeViewRepeat.HideSelection = false; // keep selection when focus is lost

            while (currentCC != null)
            {
                if (currentCC.Tag.Contains("od:repeat"))
                {
                    TagData td = new TagData(currentCC.Tag);
                    string ancestorRepeatXPathID = td.getRepeatID();

                    // Find associated question
                    xpathsXpath xp = xppe.getXPathByID(ancestorRepeatXPathID);
                    question q = questionnaire.getQuestion(xp.questionID);

                    thisNode = new TreeNode(q.text);
                    thisNode.Tag = ancestorRepeatXPathID;

                    if (previousNode == null)
                    {
                        // Check the innermost
                        treeViewRepeat.SelectedNode = thisNode;
                    }
                    else
                    {
                        thisNode.Nodes.Add(previousNode);
                    }
                    previousNode = thisNode;
                }
                currentCC = currentCC.ParentContentControl;
            }

            if (thisNode == null)
            {
                // Hide the control
            //                this.groupBoxRepeat.Visible = false;
                root = null;
            }
            else
            {
                root.Nodes.Add(thisNode);
                this.treeViewRepeat.Nodes.Add(root);
                treeViewRepeat.ExpandAll();
            }
        }
コード例 #18
0
        private void init1()
        {
            FabDocxState fabDocxState = (FabDocxState)Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject(Globals.Factory).Tag;
            this.model = fabDocxState.model;
            xppe = new XPathsPartEntry(model);

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

            this.answersPart = model.answersPart;
        }
コード例 #19
0
        public FormAnswerOrder(Office.CustomXMLPart answersPart, Office.CustomXMLPart questionsPart)
        {
            InitializeComponent();

            // To populate the tree view, we need to traverse
            // the answers.  We could do this at the DOM level,
            // or using our answers object.
            // Best to use our answers object.

            this.answersPart = answersPart;
            answersObj = new answers();
            OpenDope_AnswerFormat.answers.Deserialize(answersPart.XML, out answersObj);

            // We want to show the question text in the tree view
            questionnaire = new questionnaire();
            questionnaire.Deserialize(questionsPart.XML, out questionnaire);

            ImageList TreeviewIL = new ImageList();
            TreeviewIL.Images.Add(System.Drawing.Image.FromStream(
                System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenDope_AnswerFormat.folder.png")));
            TreeviewIL.Images.Add(System.Drawing.Image.FromStream(
                System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenDope_AnswerFormat.Icons.LogicTree.variable_chevron.png")));
            TreeviewIL.Images.Add(System.Drawing.Image.FromStream(
                System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenDope_AnswerFormat.Icons.LogicTree.repeat.png")));
            this.treeView1.ImageList = TreeviewIL;

            addNodes(answersObj.Items, root);
            this.treeView1.Nodes.Add(root);
            root.ImageIndex = 0;
            root.SelectedImageIndex = 0;

            treeView1.ExpandAll();
        }
コード例 #20
0
        public FormCondition(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 = new questionnaire();
            questionnaire.Deserialize(questionsPart.XML, out questionnaire);

            questionListHelper = new Helpers.QuestionListHelperForConditionsForm(model, xppe, questionnaire, cc);
            questionListHelper.listBoxTypeFilter = listBoxTypeFilter;
            questionListHelper.listBoxQuestions = listBoxQuestions;
            questionListHelper.checkBoxScope = checkBoxScope;

            questionListHelper.comboBoxValues = comboBoxValues;
            questionListHelper.listBoxPredicate = listBoxPredicate;

            this.listBoxQuestions.SelectedIndexChanged += new System.EventHandler(questionListHelper.listBoxQuestions_SelectedIndexChanged);
            this.listBoxTypeFilter.SelectedIndexChanged += new System.EventHandler(questionListHelper.listBoxTypeFilter_SelectedIndexChanged);

            question existingQuestion = null;
            string matchResponse = null;
            if (existingCondition != null)
            {
                // Use the question associated with it, to pre-select
                // the correct entries in the dialog.

                // Re-label the window, so user can see what the condition was about
                this.Text = "Editing Condition:   " + cc.Title;

                //List<xpathsXpath> xpaths = ConditionsPartEntry.getXPathsUsedInCondition(existingCondition, xppe);
                List<xpathsXpath> xpaths = new List<xpathsXpath>();
                existingCondition.listXPaths(xpaths, cpe.conditions, xppe.getXPaths());

                if (xpaths.Count > 1)
                {
                    // TODO: use complex conditions editor
                }
                xpathsXpath xpathObj = xpaths[0];

                String xpathVal = xpathObj.dataBinding.xpath;

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

                    existingQuestion = this.questionnaire.getQuestion(xpathObj.questionID);
                    matchResponse = xpathVal;
                }
                else if (xpathVal.Contains("position"))
                {
                    // TODO
                }
                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);

                    existingQuestion = this.questionnaire.getQuestion(qid);
                    matchResponse = xpathVal;

                }

            }

            questionListHelper.populateTypeFilter(true);

            if (existingQuestion == null)
            {
                // for init, populate with all questions
                questionListHelper.populateQuestions(null);
            }
            else
            {
                // Just show the existing question
                listBoxQuestions.Items.Add(existingQuestion);
            }

            if (this.listBoxQuestions.Items.Count == 0) // Never happens if in a repeat, and nor do we want it to, since user might just want to use "repeat pos" stuff
            {
                // Try including out of scope
                this.checkBoxScope.Checked = true;
                questionListHelper.populateQuestions(null);
                if (this.listBoxQuestions.Items.Count == 0)
                {
                    MessageBox.Show("You can't define a condition until you have set up at least one question. Let's do that now. ");

                    FormQA formQA = new FormQA(cc, false);
                    formQA.ShowDialog();
                    formQA.Dispose();

                    // Refresh these
                    xppe = new XPathsPartEntry(model);
                    questionnaire.Deserialize(questionsPart.XML, out questionnaire);

                    questionListHelper.filterAction();

                    return;
                }
            }
            // value
            question q;
            if (existingQuestion == null)
            {
                // for init, populate with all questions
                q = (question)this.listBoxQuestions.Items[0];
            }
            else
            {
                q = existingQuestion;
            }

            this.listBoxQuestions.SelectedItem = q;
            if (q.response.Item is responseFixed)
            {
                questionListHelper.populateValues((responseFixed)q.response.Item, matchResponse);
            }

            // predicate =
            questionListHelper.populatePredicates(q);  // TODO: set this correctly in editing mode
        }
コード例 #21
0
 public static bool LoadFromFile(string fileName, out questionnaire obj)
 {
     System.Exception exception = null;
     return LoadFromFile(fileName, out obj, out exception);
 }
コード例 #22
0
        /// <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());
        }