public void DoInsert()
{
if (this.m_template == null)
return;
if (this.treeViewTemplate.Nodes.Count == 0)
{
DocEntity docEntityBase = null;
if (this.m_parent is DocTemplateDefinition)
{
string classname = ((DocTemplateDefinition)this.m_parent).Type;
docEntityBase = this.m_project.GetDefinition(classname) as DocEntity;
}
// get selected entity
DocEntity docEntityThis = this.m_project.GetDefinition(this.m_template.Type) as DocEntity;
using (FormSelectEntity form = new FormSelectEntity(docEntityBase, docEntityThis, this.m_project, SelectDefinitionOptions.Entity))
{
DialogResult res = form.ShowDialog(this);
if (res == DialogResult.OK && form.SelectedEntity != null)
{
this.m_template.Type = form.SelectedEntity.Name;
this.LoadTemplateGraph();
this.ContentChanged(this, EventArgs.Empty);
}
}
return;
}
if (this.m_attribute != null && !String.IsNullOrEmpty(this.m_attribute.DefinedType))
{
DocTemplateDefinition docTemplate = this.m_template;
DocAttribute docAttribute = this.m_attribute;
string entityname = null;
switch (this.m_attribute.DefinedType)
{
case "BOOLEAN":
case "LOGICAL":
case "BINARY":
case "STRING":
case "REAL":
case "INTEGER":
case "NUMBER":
entityname = this.m_attribute.DefinedType;
break;
default:
{
// qualify schema
DocObject docobj = null;
if (!String.IsNullOrEmpty(this.m_template.Code))
{
foreach(DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
if (docSchema.Name.Equals(this.m_template.Code, StringComparison.OrdinalIgnoreCase))
{
docobj = docSchema.GetDefinition(docAttribute.DefinedType);
break;
}
}
}
}
if (docobj == null)
{
docobj = this.m_project.GetDefinition(docAttribute.DefinedType);
}
using (FormSelectEntity form = new FormSelectEntity((DocDefinition)docobj, null, this.m_project, SelectDefinitionOptions.Entity | SelectDefinitionOptions.Type))
{
DialogResult res = form.ShowDialog(this);
if (res == DialogResult.OK && form.SelectedEntity != null)
{
entityname = form.SelectedEntity.Name;
}
}
break;
}
}
if (entityname != null)
{
// get or add attribute rule
TreeNode tn = this.treeViewTemplate.SelectedNode;
DocModelRuleAttribute docRuleAtt = null;
if (this.treeViewTemplate.SelectedNode.Tag is DocModelRuleAttribute)
{
docRuleAtt = (DocModelRuleAttribute)this.treeViewTemplate.SelectedNode.Tag;
}
else
{
docRuleAtt = new DocModelRuleAttribute();
docRuleAtt.Name = docAttribute.Name;
if (this.treeViewTemplate.SelectedNode.Tag is DocModelRuleEntity)
{
DocModelRuleEntity docRuleEnt = (DocModelRuleEntity)this.treeViewTemplate.SelectedNode.Tag;
docRuleEnt.Rules.Add(docRuleAtt);
}
else if (this.treeViewTemplate.SelectedNode.Tag is DocTemplateDefinition)
{
docTemplate.Rules.Add(docRuleAtt);
}
tn = this.LoadTemplateGraph(tn, docRuleAtt);
}
// get and add entity rule
DocModelRuleEntity docRuleEntity = new DocModelRuleEntity();
docRuleEntity.Name = entityname;
docRuleAtt.Rules.Add(docRuleEntity);
this.treeViewTemplate.SelectedNode = this.LoadTemplateGraph(tn, docRuleEntity);
// copy to child templates
docTemplate.PropagateRule(this.treeViewTemplate.SelectedNode.FullPath);
this.m_selection = docRuleEntity;
this.ContentChanged(this, EventArgs.Empty);
this.SelectionChanged(this, EventArgs.Empty);
}
}
else
{
// pick attribute, including attribute that may be on subtype
DocModelRule rule = null;
if (this.treeViewTemplate.SelectedNode != null)
{
rule = this.treeViewTemplate.SelectedNode.Tag as DocModelRule;
}
//if (rule == null)
// return;
DocTemplateDefinition docTemplate = (DocTemplateDefinition)this.m_template;
string typename = null;
if (rule is DocModelRuleEntity)
{
DocModelRuleEntity docRuleEntity = (DocModelRuleEntity)rule;
typename = docRuleEntity.Name;
}
else
{
// get applicable entity of target (or parent entity rule)
typename = docTemplate.Type;
}
DocEntity docEntity = this.m_project.GetDefinition(typename) as DocEntity;
if (docEntity == null)
{
#if false // constraints now edited at operations
// launch dialog for constraint
using (FormConstraint form = new FormConstraint())
{
DialogResult res = form.ShowDialog(this);
if (res == DialogResult.OK)
{
DocModelRuleConstraint docRuleConstraint = new DocModelRuleConstraint();
rule.Rules.Add(docRuleConstraint);
docRuleConstraint.Description = form.Expression;
docRuleConstraint.Name = form.Expression; // for viewing
this.treeViewTemplate.SelectedNode = this.LoadTemplateGraph(this.treeViewTemplate.SelectedNode, docRuleConstraint);
// copy to child templates
docTemplate.PropagateRule(this.treeViewTemplate.SelectedNode.FullPath);
}
}
#endif
}
else
{
// launch dialog to pick attribute of entity
using (FormSelectAttribute form = new FormSelectAttribute(docEntity, this.m_project, null, true))
{
DialogResult res = form.ShowDialog(this);
if (res == DialogResult.OK && form.Selection != null)
{
// then add and update tree
DocModelRuleAttribute docRuleAttr = new DocModelRuleAttribute();
docRuleAttr.Name = form.Selection;
if (rule != null)
{
rule.Rules.Add(docRuleAttr);
}
else
{
docTemplate.Rules.Add(docRuleAttr);
}
this.treeViewTemplate.SelectedNode = this.LoadTemplateGraph(this.treeViewTemplate.SelectedNode, docRuleAttr);
// copy to child templates
docTemplate.PropagateRule(this.treeViewTemplate.SelectedNode.FullPath);
}
}
}
}
}