public override void ToJSON( JObject ParentObject )
        {
            CswNbtNode RelatedNode = null;
            if( CswTools.IsPrimaryKey( RelatedNodeId ) )
            {
                //ParentObject[_NodeIDSubField.ToXmlNodeName( true ).ToLower()] = RelatedNodeId.ToString();
                RelatedNode = _CswNbtResources.Nodes[RelatedNodeId];
            }

            if( IsEditModeEditable )
            {
                JArray JOptions = (JArray) ( ParentObject["options"] = new JArray() );
                bool AllowEdit = _CswNbtResources.Permit.isPropWritable( CswEnumNbtNodeTypePermission.Create, NodeTypeProp, null );
                Int32 OptionCount = 0;
                if( AllowEdit )
                {
                    CswPrimaryKey pk = null;
                    if( null != RelatedNode )
                    {
                        pk = RelatedNode.NodeId;
                    }
                    //Dictionary<CswPrimaryKey, string> Options = _OptionsOverride ?? _getOptions( _CswNbtResources, _CswNbtMetaDataNodeTypeProp.IsRequired, _CswNbtMetaDataNodeTypeProp.FKValue, pk, View );
                    Dictionary<CswPrimaryKey, string> Options = _OptionsOverride ?? _getOptions( _CswNbtResources,
                                                                                                 CswConvert.ToBoolean( _CswNbtNodePropData[CswNbtFieldTypeRuleRelationship.AttributeName.Required] ),
                                                                                                 CswConvert.ToInt32( _CswNbtNodePropData[CswNbtFieldTypeRuleRelationship.AttributeName.Target, CswNbtFieldTypeRuleMetaDataList.SubFieldName.Id] ),
                                                                                                 pk, View );
                    if( Options.Count > _SearchThreshold )
                    {
                        ParentObject["usesearch"] = true;
                    }
                    else
                    {
                        ParentObject["usesearch"] = false;
                        CswPrimaryKey FirstPk = null;
                        foreach( CswPrimaryKey NodePk in Options.Keys )
                        {
                            if( CswTools.IsPrimaryKey( NodePk ) )
                            {
                                OptionCount += 1;
                                JObject JOption = new JObject();
                                FirstPk = FirstPk ?? NodePk;
                                JOption["id"] = NodePk.ToString();
                                JOption["value"] = Options[NodePk];
                                JOption["link"] = CswNbtNode.getNodeLink( NodePk, Options[NodePk] );
                                JOptions.Add( JOption );
                            }
                        }

                        //If Required, show empty when no value is selected and if there is more than one option to select from
                        bool ShowEmptyOption = Required && false == CswTools.IsPrimaryKey( RelatedNodeId ) && 1 < OptionCount;
                        //If not Required, always show the empty value
                        ShowEmptyOption = ShowEmptyOption || false == Required;

                        if( ShowEmptyOption )
                        {
                            JObject JOption = new JObject();
                            JOption["id"] = "";
                            JOption["value"] = "";
                            JOptions.AddFirst( JOption );
                        }
                        else
                        {
                            //Case 30030
                            if( null == RelatedNode &&
                                Required &&
                                Options.Count == 1 &&
                                CswTools.IsPrimaryKey( FirstPk ) )
                            {
                                RelatedNode = _CswNbtResources.Nodes[FirstPk];
                            }
                        }
                    }
                }

                ParentObject["nodetypeid"] = 0;
                ParentObject["objectclassid"] = 0;
                ParentObject["propertysetid"] = 0;
                bool AllowAdd = false;
                bool AllowView = true;
                if( null != RelatedNodeId )
                {
                    CswNbtMetaDataNodeType TargetNodeType = _CswNbtResources.MetaData.getNodeTypeFromNodeId( RelatedNodeId );
                    AllowAdd = ( null != TargetNodeType && _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.Create, TargetNodeType ) );
                    AllowView = ( null != TargetNodeType && _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.View, TargetNodeType ) );
                }
                else if( TargetType == CswEnumNbtViewRelatedIdType.NodeTypeId )
                {
                    ParentObject["nodetypeid"] = TargetId;
                    CswNbtMetaDataNodeType TargetNodeType = _CswNbtResources.MetaData.getNodeType( TargetId );
                    AllowAdd = ( null != TargetNodeType && _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.Create, TargetNodeType ) );
                    AllowView = ( null != TargetNodeType && _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.View, TargetNodeType ) );
                }
                else if( TargetType == CswEnumNbtViewRelatedIdType.ObjectClassId )
                {
                    ParentObject["objectclassid"] = TargetId;
                    CswNbtMetaDataObjectClass TargetObjectClass = _CswNbtResources.MetaData.getObjectClass( TargetId );
                    AllowAdd = ( null != TargetObjectClass &&
                                 TargetObjectClass.CanAdd &&
                                 TargetObjectClass.getNodeTypes().Any( nt => _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.Create, nt ) ) );
                    if( false == AllowAdd )
                    {
                        AllowView = ( null != TargetObjectClass &&
                                      TargetObjectClass.getNodeTypes().Any( nt => _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.View, nt ) ) );
                    }
                }
                else if( TargetType == CswEnumNbtViewRelatedIdType.PropertySetId )
                {
                    ParentObject["propertysetid"] = TargetId;
                    CswNbtMetaDataPropertySet TargetPropSet = _CswNbtResources.MetaData.getPropertySet( TargetId );
                    AllowAdd = TargetPropSet.getObjectClasses().Any( oc => null != oc &&
                                                                           oc.CanAdd &&
                                                                           oc.getNodeTypes().Any( nt => _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.Create, nt ) ) );
                    if( false == AllowAdd )
                    {
                        AllowView = TargetPropSet.getObjectClasses().Any( oc => null != oc &&
                                                                                oc.getNodeTypes().Any( nt => _CswNbtResources.Permit.canNodeType( CswEnumNbtNodeTypePermission.View, nt ) ) );
                    }
                }
                ParentObject["allowadd"] = AllowAdd;
                ParentObject["allowview"] = AllowView;

                if( null != View )
                {
                    ParentObject["viewid"] = View.ViewId.ToString();
                    if( AllowEdit && OptionCount == 0 )
                    {
                        ParentObject["doGetNodes"] = true;
                    }
                }
            } // if( ForEdit )

            ParentObject[_NodeIDSubField.ToXmlNodeName( true ).ToLower()] = string.Empty;
            ParentObject[_NameSubField.ToXmlNodeName( true ).ToLower()] = CachedNodeName;
            ParentObject["relatednodeid"] = string.Empty;
            ParentObject["relatednodelink"] = string.Empty;
            if( null != RelatedNode )
            {
                ParentObject[_NodeIDSubField.ToXmlNodeName( true ).ToLower()] = RelatedNode.NodeId.ToString();
                ParentObject[_NameSubField.ToXmlNodeName( true ).ToLower()] = RelatedNode.NodeName;
                ParentObject["relatednodeid"] = RelatedNode.NodeId.ToString();
                ParentObject["relatednodelink"] = RelatedNode.NodeLink;
            }
        } // ToJSON()
Ejemplo n.º 2
0
        }             // _getOptions()

        #endregion Relationship Properties and Functions

        #region Serialization

        public override void ToJSON(JObject ParentObject)
        {
            bool allowAdd = false;

            ParentObject["nodetypeid"]    = string.Empty;
            ParentObject["objectclassid"] = string.Empty;

            if (RelationshipType == CswEnumNbtViewPropIdType.NodeTypePropId)
            {
                CswNbtMetaDataNodeTypeProp RelationshipNTP = _CswNbtResources.MetaData.getNodeTypeProp(RelationshipId);
                CswNbtMetaDataNodeType     RelationshipNT  = RelationshipNTP.getNodeType();
                if (null != RelationshipNT)
                {
                    ParentObject["nodetypeid"] = RelationshipNT.NodeTypeId;
                    CswNbtMetaDataObjectClass RelationshipOC = RelationshipNT.getObjectClass();
                    if (null != RelationshipOC)
                    {
                        ParentObject["objectclassid"] = RelationshipOC.ObjectClassId;
                        allowAdd = (RelationshipOC.CanAdd &&
                                    _CswNbtResources.Permit.canNodeType(CswEnumNbtNodeTypePermission.Create, RelationshipNT));
                    }
                }
            }
            else if (RelationshipType == CswEnumNbtViewPropIdType.ObjectClassPropId)
            {
                CswNbtMetaDataObjectClassProp RelationshipOCP = _CswNbtResources.MetaData.getObjectClassProp(RelationshipId);
                CswNbtMetaDataObjectClass     RelationshipOC  = RelationshipOCP.getObjectClass();
                if (null != RelationshipOC)
                {
                    ParentObject["objectclassid"] = RelationshipOC.ObjectClassId;
                    allowAdd = RelationshipOC.CanAdd;
                    if (allowAdd)
                    {
                        foreach (CswNbtMetaDataNodeType NodeType in RelationshipOC.getLatestVersionNodeTypes())
                        {
                            allowAdd = _CswNbtResources.Permit.canNodeType(CswEnumNbtNodeTypePermission.Create, NodeType);
                            if (allowAdd)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            ParentObject["allowadd"] = allowAdd;

            JArray JOptions = new JArray();

            ParentObject["options"]         = JOptions;
            ParentObject["relatednodeid"]   = string.Empty;
            ParentObject["relatednodename"] = string.Empty;

            Dictionary <CswPrimaryKey, string> Options = _getOptions();
            CswPrimaryKey SelectedNodeId = null;

            if (null != SetSelected)
            {
                SelectedNodeId = SetSelected();
            }
            bool first = true;

            foreach (CswPrimaryKey NodePk in Options.Keys)
            {
                if (first || (null != NodePk && NodePk == SelectedNodeId))
                {
                    // Choose first option by default
                    ParentObject["relatednodeid"]   = NodePk.ToString();
                    ParentObject["relatednodename"] = Options[NodePk];
                    first = false;
                }
                if (NodePk != null && NodePk.PrimaryKey != Int32.MinValue)
                {
                    JObject JOption = new JObject();
                    JOption["id"]    = NodePk.ToString();
                    JOption["value"] = Options[NodePk];
                    JOptions.Add(JOption);
                }
            }
        } // ToJson()