private static string GenerateApiMethods(MFilesObject obj, Api api)
        {
            StringBuilder apiMethodStrBldr = new StringBuilder();

            apiMethodStrBldr.Append(GenerateApiClassConstructorMethod(obj, api));

            string ot_class_name = $"OT_{obj.Name.Normalize_to_cs_type()}";

            // API Create method definition
            apiMethodStrBldr.AppendLine("\t\tpublic bool create(int? createdby_mfuserid = null) {");
            apiMethodStrBldr.AppendLine("\t\t\treturn objVerEx.create(TypeID, createdby_mfuserid);");
            apiMethodStrBldr.AppendLine("\t\t}");

            // API Get method definition
            apiMethodStrBldr.AppendLine($"\t\tpublic static {ot_class_name} get(int objId, bool includeDeleted = false) {{");
            apiMethodStrBldr.AppendLine($"\t\t\treturn APIExtensions.search<{ot_class_name}>(MFilesAPIConnection.Vault, TypeID, objId, includeDeleted);");
            apiMethodStrBldr.AppendLine("\t\t}");

            // API Get All method definition
            apiMethodStrBldr.AppendLine($"\t\tpublic static List<{ot_class_name}> getAll(bool includeDeleted = false, int searchResultsCount = 500) {{");
            apiMethodStrBldr.AppendLine($"\t\t\treturn APIExtensions.getAll<{ot_class_name}>(MFilesAPIConnection.Vault, TypeID, includeDeleted, searchResultsCount);");
            apiMethodStrBldr.AppendLine("\t\t}");

            apiMethodStrBldr.AppendLine($"\t\tpublic static List<{ot_class_name}> search(Action<SearchParameters> parameters, int? classId = null, bool includeDeleted = false, int max_result_count=500, int search_timeout_seconds = 60) {{");
            apiMethodStrBldr.AppendLine($"\t\t\treturn APIExtensions.search<{ot_class_name}>(MFilesAPIConnection.Vault, parameters, classId, includeDeleted, max_result_count, search_timeout_seconds);");
            apiMethodStrBldr.AppendLine("\t\t}");

            return(apiMethodStrBldr.ToString());
        }
예제 #2
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            MFilesObject that = obj as MFilesObject;

            return(Id == that.Id);
        }
        private static string GenerateApiClassConstructorMethod(MFilesObject obj, Api api)
        {
            StringBuilder apiClassCnstrctrStrBldr = new StringBuilder();
            Dictionary <int, List <PropertyDefinition> > classesAndRequiredPropDefs =
                api.PropertyDefinitions.ClassPropertyDefinitions(obj.Id);
            Dictionary <int, ClassType> classDefs   = api.ClassTypes.ClassTypesDict;
            List <PropertyDefinition>   reqPropDefs = null;
            ClassType classDef = null;

            foreach (KeyValuePair <int, List <PropertyDefinition> > classAndReqPropDefs in classesAndRequiredPropDefs)
            {
                reqPropDefs = classAndReqPropDefs.Value;
                if (reqPropDefs != null && reqPropDefs.Count() > 0)
                {
                    classDef = classDefs[classAndReqPropDefs.Key];
                    apiClassCnstrctrStrBldr.Append($"\t\tpublic static OT_{obj.Name.Normalize_to_cs_type()} Create_{classDef.Name.Normalize_to_cs_type(true)}(\n\t\t\t");
                    apiClassCnstrctrStrBldr.AppendLine(string.Join(",\n\t\t\t",
                                                                   reqPropDefs.Select(propDef => Common.Return_type(propDef) + " " + propDef.Name.Normalize_to_cs_type(true))));

                    if ("Document" == obj.Name.Normalize_to_cs_type())
                    {
                        apiClassCnstrctrStrBldr.AppendLine("\t\t\t\n, string fileUploadPath");
                    }
                    apiClassCnstrctrStrBldr.AppendLine("\t\t\t,int? createdby_mfuserid = null");
                    apiClassCnstrctrStrBldr.AppendLine("\t\t) {");
                    apiClassCnstrctrStrBldr.AppendLine(
                        $"\t\t\tOT_{obj.Name.Normalize_to_cs_type()} objInstance = new OT_{obj.Name.Normalize_to_cs_type()}() {{ objVerEx = new ObjVerEx(MFilesAPIConnection.Vault, new ObjectVersion(), new PropertyValues()) }};");
                    apiClassCnstrctrStrBldr.AppendLine($"\t\t\tobjInstance.Class = VL_Class.{classDef.Name.Normalize_to_cs_type(true)};");

                    var workflow = $"Enum.TryParse(\"{classDef.WorkflowId}\", out VL_Workflow wf) == true ? wf : VL_Workflow._None_";
                    if (classDef.WorkflowId != 0)
                    {
                        apiClassCnstrctrStrBldr.AppendLine($"\t\t\tobjInstance.Workflow = {workflow};");
                    }

                    reqPropDefs.ForEach(propDef => apiClassCnstrctrStrBldr.AppendLine($"\t\t\tobjInstance.{propDef.Name.Normalize_to_cs_type(true)} = {propDef.Name.Normalize_to_cs_type(true)};"));
                    if ("Document" == obj.Name.Normalize_to_cs_type())
                    {
                        apiClassCnstrctrStrBldr.AppendLine("\t\t\treturn objInstance.create_singlefile_doc(fileUploadPath, createdby_mfuserid);");
                    }
                    else
                    {
                        apiClassCnstrctrStrBldr.AppendLine("\t\t\tobjInstance.create(createdby_mfuserid);");
                        apiClassCnstrctrStrBldr.AppendLine($"\t\t\tobjInstance = new OT_{obj.Name.Normalize_to_cs_type()} {{ objVerEx = new ObjVerEx(objInstance.objVerEx.Vault, objInstance.objVerEx.ObjVer) }};");
                        apiClassCnstrctrStrBldr.AppendLine("\t\t\treturn objInstance;");
                    }

                    apiClassCnstrctrStrBldr.AppendLine("\t\t}");
                }
            }

            classesAndRequiredPropDefs =
                api.PropertyDefinitions.ClassPropertyDefinitions(obj.Id, false);

            foreach (KeyValuePair <int, List <PropertyDefinition> > classAndReqPropDefs in classesAndRequiredPropDefs)
            {
                reqPropDefs = classAndReqPropDefs.Value;
                if (reqPropDefs != null && reqPropDefs.Count() > 0)
                {
                    classDef = classDefs[classAndReqPropDefs.Key];
                    apiClassCnstrctrStrBldr.Append($"\t\tpublic static OT_{obj.Name.Normalize_to_cs_type()} Create_{classDef.Name.Normalize_to_cs_type(true)}_Class(\n\t\t\t");
                    apiClassCnstrctrStrBldr.AppendLine(string.Join(",\n\t\t\t",
                                                                   reqPropDefs.Select(propDef => Common.Return_type(propDef) + " " + propDef.Name.Normalize_to_cs_type(true))));
                    if ("Document" == obj.Name.Normalize_to_cs_type())
                    {
                        apiClassCnstrctrStrBldr.AppendLine("\t\t\t\n, string fileUploadPath");
                    }
                    apiClassCnstrctrStrBldr.AppendLine("\t\t\t,int? createdby_mfuserid = null");
                    apiClassCnstrctrStrBldr.AppendLine("\t\t) {");
                    apiClassCnstrctrStrBldr.AppendLine($"\t\t\tOT_{obj.Name.Normalize_to_cs_type()} objInstance = new OT_{obj.Name.Normalize_to_cs_type()}() {{ objVerEx = new ObjVerEx(MFilesAPIConnection.Vault, new ObjectVersion(), new PropertyValues()) }};");
                    apiClassCnstrctrStrBldr.AppendLine(
                        $"\t\t\tobjInstance.Class = VL_Class.{classDef.Name.Normalize_to_cs_type(true)};");
                    reqPropDefs.ForEach(propDef => apiClassCnstrctrStrBldr.AppendLine($"\t\t\tobjInstance.{propDef.Name.Normalize_to_cs_type(true)} = {propDef.Name.Normalize_to_cs_type(true)};"));
                    apiClassCnstrctrStrBldr.AppendLine("\t\t\tobjInstance.create(createdby_mfuserid);");
                    apiClassCnstrctrStrBldr.AppendLine($"\t\t\tobjInstance = new OT_{obj.Name.Normalize_to_cs_type()} {{ objVerEx = new ObjVerEx(objInstance.objVerEx.Vault, objInstance.objVerEx.ObjVer) }};");
                    if ("Document" == obj.Name.Normalize_to_cs_type())
                    {
                        apiClassCnstrctrStrBldr.AppendLine($"\t\t\tobjInstance.upload_file<OT_Document>(fileUploadPath);");
                    }
                    apiClassCnstrctrStrBldr.AppendLine("\t\t\treturn objInstance;");
                    apiClassCnstrctrStrBldr.AppendLine("\t\t}");
                }
            }

            return(apiClassCnstrctrStrBldr.ToString());
        }
 private static void GenerateDocumentOnlyPropertyDefinitions(StringBuilder sb, MFilesObject obj)
 {
     if ("Document" == obj.Name.Normalize_to_cs_type())
     {
         sb.AppendLine("\t\tpublic bool? isTemplate {");
         sb.AppendLine("\t\t\tget { return objVerEx.GetPropertyBooleanValue(PD_Is_template.id); }");
         sb.AppendLine("\t\t\tset { objVerEx.SetProperty(Extensions.ConvertPropertyBoolValue(PD_Is_template.id, MFDataType.MFDatatypeBoolean, value)); }");
         sb.AppendLine("\t\t}");
         sb.AppendLine("\t\tpublic void download_file(string download_path) {");
         sb.AppendLine("\t\t\tAPIExtensions.download_file(MFilesAPIConnection.Vault, ID, download_path);");
         sb.AppendLine("\t\t}");
         sb.AppendLine("\t\tpublic static void download_file(int object_id, string download_path) {");
         sb.AppendLine("\t\t\tAPIExtensions.download_file(MFilesAPIConnection.Vault, object_id, download_path);");
         sb.AppendLine("\t\t}");
         sb.AppendLine($"\t\tpublic OT_{obj.Name.Normalize_to_cs_type()} upload_file(string upload_path) {{");
         sb.AppendLine($"\t\t\treturn this.upload_file<OT_{obj.Name.Normalize_to_cs_type()}>(upload_path);");
         sb.AppendLine("\t\t}");
         sb.AppendLine("\t\tpublic void perform_ocr() {");
         sb.AppendLine("\t\t\tthis.perform_ocr();");
         sb.AppendLine("\t\t}");
     }
 }
        private static string GenerateObjects(MFilesObject obj, Api api)
        {
            Dictionary <int, PropertyDefinition> .ValueCollection props = api.PropertyDefinitions.ObjectPropertyDefinitions(obj.Id).Values;
            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"\n\tpublic class OT_{obj.Name.Normalize_to_cs_type()} : IObjVerEx, IAPIOperations");
            sb.AppendLine("\t{");
            sb.AppendLine($"\t\tpublic const int TypeID = {obj.Id};");
            sb.AppendLine("\t\tpublic int ID { get { return objVerEx.ObjID.ID; } }");
            sb.AppendLine("\t\tpublic VL_Class Class {");
            sb.AppendLine("\t\t\tget { return (VL_Class)Enum.ToObject(typeof(VL_Class), objVerEx.Class); }");
            sb.AppendLine("\t\t\tset { objVerEx.SetProperty(PD_Class.id, PD_Class.data_type, value); }");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic ObjVerEx objVerEx { get; set; }");
            sb.AppendLine("\t\tDictionary<int,object> _object_properties_cache = new Dictionary<int,object>();");
            sb.AppendLine("\t\tDictionary<int, object> IObjVerEx.object_properties_cache {");
            sb.AppendLine("\t\t\tget { return _object_properties_cache; }");
            sb.AppendLine("\t\t\tset { _object_properties_cache = value; }");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic bool IsDeleted { get { return objVerEx.Deleted(); } }");
            sb.AppendLine("\t\tpublic bool IsDestroyed { get { return objVerEx.IsDestroyed; } }");
            sb.AppendLine("\t\tpublic bool IsExternal {");
            sb.AppendLine("\t\t\tget {");
            sb.AppendLine("\t\t\t\tObjType objType = objVerEx.Vault.ObjectTypeOperations.GetObjectType(TypeID);");
            sb.AppendLine("\t\t\t\treturn objType.External;");
            sb.AppendLine("\t\t\t}");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic DateTime CreatedDate {");
            sb.AppendLine("\t\t\tget { return objVerEx.GetProperyDateTimeValue(PD_Created.id); }");
            sb.AppendLine("\t\t}");
            GenerateDocumentOnlyPropertyDefinitions(sb, obj);
            sb.AppendLine("\t\tpublic VL_NACLs? NamedACL {");
            sb.AppendLine("\t\t\tget {");
            sb.AppendLine("\t\t\t\tif (objVerEx.Permissions.NamedACL.NamedACLType == MFNamedACLType.MFNamedACLTypeNone) return null;");
            sb.AppendLine("\t\t\t\treturn (VL_NACLs)Enum.ToObject(typeof(VL_NACLs), objVerEx.Permissions.NamedACL.ID);");
            sb.AppendLine("\t\t\t}");
            sb.AppendLine("\t\t\tset { this.SetNamedACL(value); }");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic AccessControlList CustomACL {");
            sb.AppendLine("\t\t\tget {");
            sb.AppendLine("\t\t\t\tif (!HasCustomACL) return null;");
            sb.AppendLine("\t\t\t\treturn objVerEx.Permissions.AccessControlList;");
            sb.AppendLine("\t\t\t}");
            sb.AppendLine("\t\t\tset { this.SetAccessControlList(value); }");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic bool HasCustomACL => objVerEx.Permissions.CustomACL;");
            sb.AppendLine($"\t\tpublic OT_{obj.Name.Normalize_to_cs_type()} PreviousVersion {{");
            sb.AppendLine("\t\t\tget {");
            sb.AppendLine("\t\t\t\tif (objVerEx == null || objVerEx.PreviousVersion == null)");
            sb.AppendLine("\t\t\t\t\treturn null;");
            sb.AppendLine("\t\t\t\tif (objVerEx.Version == 1)");
            sb.AppendLine("\t\t\t\t\treturn this;");
            sb.AppendLine($"\t\t\t\treturn new OT_{obj.Name.Normalize_to_cs_type()}() {{ objVerEx = objVerEx.PreviousVersion }};");
            sb.AppendLine("\t\t\t}");
            sb.AppendLine("\t\t}");
            sb.AppendLine($"\t\tpublic OT_{obj.Name.Normalize_to_cs_type()}() {{ }}");
            sb.AppendLine("\t\tpublic override bool Equals(object obj)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tif (obj == null)");
            sb.AppendLine("\t\t\t\treturn false;");
            sb.AppendLine($"\t\t\tOT_{obj.Name.Normalize_to_cs_type()} that = obj as OT_{obj.Name.Normalize_to_cs_type()};");
            sb.AppendLine("\t\t\treturn ID == that.ID;");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic override int GetHashCode() {");
            sb.AppendLine("\t\t\treturn ID;");
            sb.AppendLine("\t\t}");
            sb.AppendLine($"\t\tpublic static bool operator ==(OT_{obj.Name.Normalize_to_cs_type()} lhs, OT_{obj.Name.Normalize_to_cs_type()} rhs) {{");
            sb.AppendLine("\t\t\tif (object.ReferenceEquals(lhs, null))");
            sb.AppendLine("\t\t\t\treturn object.ReferenceEquals(rhs, null);");
            sb.AppendLine("\t\t\treturn lhs.Equals(rhs);");
            sb.AppendLine("\t\t}");
            sb.AppendLine($"\t\tpublic static bool operator !=(OT_{obj.Name.Normalize_to_cs_type()} lhs, OT_{obj.Name.Normalize_to_cs_type()} rhs) {{");
            sb.AppendLine("\t\t\treturn !(lhs==rhs);");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic void Refresh() {");
            sb.AppendLine("\t\t\tthis.ClearAllCachedProperties();");
            sb.AppendLine("\t\t\tobjVerEx.Refresh();");
            sb.AppendLine("\t\t}");
            sb.AppendLine($"\t\tpublic static List<OT_{obj.Name.Normalize_to_cs_type()}> indirect(IObjVerEx obj)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\tif (obj == null || obj.objVerEx == null || obj.objVerEx.Deleted())");
            sb.AppendLine($"\t\t\t\treturn new List<OT_{obj.Name.Normalize_to_cs_type()}>();");
            sb.AppendLine($"\t\t\treturn obj.objVerEx.GetIndirectReferenceByObject<OT_{obj.Name.Normalize_to_cs_type()}>(TypeID);");
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t\tpublic void save_properties() {");
            sb.AppendLine($"\t\t\tobjVerEx.SaveProperties();");
            sb.Append("\t\t}\n");
            sb.AppendLine("\t\tpublic int VersionNumber {");
            sb.AppendLine("\t\t\tget {");
            sb.AppendLine("\t\t\t\t return objVerEx.Version;");
            sb.AppendLine("\t\t\t}");
            sb.Append("\t\t}\n");

            bool multiselect_object_property  = false;
            bool singleselect_object_property = false;

            foreach (PropertyDefinition prop in props)
            {
                multiselect_object_property  = prop.DataType == MFDataType.MFDatatypeMultiSelectLookup && !prop.IsBasedOnValueList;
                singleselect_object_property = prop.DataType == MFDataType.MFDatatypeLookup && !prop.IsBasedOnValueList;
                var property_type = Common.Return_type(prop);
                var property_name = prop.Name.Normalize_to_cs_type();
                if (multiselect_object_property)
                {
                    property_type = $"MultiSelectOTProperty<OT_{prop.ValueListName.Normalize_to_cs_type()}>";
                }
                sb.AppendLine($"\t\tpublic {property_type} {prop.Name.Normalize_to_cs_type(true)} {{");
                sb.AppendLine($"\t\t\tget {{ return {Common.Method_name(prop)}(PD_{property_name}.id); }}");
                sb.AppendLine("\t\t\tset {");
                if (multiselect_object_property)
                {
                    sb.AppendLine($"\t\t\t\tthis.SetMultiSelectLookupValueForPropertyDefCache<OT_{prop.ValueListName.Normalize_to_cs_type(true)}>(PD_{property_name}.id, value);");
                }
                else
                {
                    if (singleselect_object_property)
                    {
                        sb.AppendLine($"\t\t\t\tthis.CacheProperty(PD_{property_name}.id, value);");
                    }
                    sb.AppendLine($"\t\t\t\tobjVerEx.SetProperty(Extensions.{Common.Convert_method(prop)}(PD_{property_name}.id, PD_{property_name}.data_type, value));");
                }
                sb.AppendLine("\t\t\t}");
                sb.AppendLine("\t\t}");
                if (prop.IsBasedOnValueList && prop.DataType != MFDataType.MFDatatypeBoolean)
                {
                    sb.Append($"\t\tpublic string {prop.Name.Normalize_to_cs_type(true)}_name => ");
                    var index = prop.DataType == MFDataType.MFDatatypeMultiSelectLookup ? "[0]" : "";
                    sb.Append($"Extensions.GetEnumDescription({Common.Method_name(prop)}(PD_{property_name}.id){index}) ");
                    sb.Append($"?? objVerEx.GetValueListItemFromVault(PD_{property_name}.id, \"{prop.ValueListName.Normalize_to_cs_type()}\");");
                    sb.AppendLine("\t\t");
                }
            }

            sb.Append(GenerateApiMethods(obj, api));
            sb.Append("\t}");

            return(sb.ToString());
        }