public static void perform_ocr(this IObjVerEx obj)
        {
            OCROptions opts = new OCROptions();

            opts.PrimaryLanguage = MFOCRLanguage.MFOCRLanguageEnglishUS;

            obj.objVerEx.CheckOut();
            ObjectFiles files = obj.objVerEx.Vault.ObjectFileOperations.GetFiles(obj.objVerEx.ObjVer);

            foreach (ObjectFile file in files)
            {
                switch (file.Extension.ToLower())
                {
                case "tif":
                case "tiff":
                case "jpg":
                case "jpeg":
                case "pdf":
                    obj.objVerEx.Vault.ObjectFileOperations.PerformOCROperation(obj.objVerEx.ObjVer, file.FileVer,
                                                                                opts, MFOCRZoneRecognitionMode.MFOCRZoneRecognitionModeNoZoneRecognition, null, true);
                    break;

                default:
                    break;
                }
            }
            obj.objVerEx.CheckIn();
        }
        public static void send_notification_email(this IObjVerEx obj, Dictionary <int, MFUserOrUserGroupType> recipients, List <string> externalRecipientsList, string subject, string body)
        {
            UserOrUserGroupIDs recipientUserOrUserGroups = new UserOrUserGroupIDs();

            if (recipients != null && recipients.Count > 0)
            {
                UserOrUserGroupID recipientUserOrUserGroup = null;

                foreach (KeyValuePair <int, MFUserOrUserGroupType> recipient in recipients)
                {
                    recipientUserOrUserGroup = new UserOrUserGroupID();
                    recipientUserOrUserGroup.UserOrGroupID   = recipient.Key;
                    recipientUserOrUserGroup.UserOrGroupType = recipient.Value;
                    recipientUserOrUserGroups.Add(-1, recipientUserOrUserGroup);
                }
            }

            Strings externalRecipients = new Strings();

            if (externalRecipientsList != null && externalRecipientsList.Count > 0)
            {
                externalRecipientsList.ForEach(externalRecipient => externalRecipients.Add(-1, externalRecipient));
            }

            obj.objVerEx.Vault.NotificationOperations.SendCustomNotification(recipientUserOrUserGroups, false, externalRecipients, true, subject, body);
        }
        public static T upload_file <T>(this IObjVerEx obj, string filePath)
            where T : IObjVerEx, new()
        {
            ObjectVersion objVersion = null;

            try {
                obj.undoObjectCheckoutIfCheckedOut();
                objVersion = obj.objVerEx.Vault.ObjectOperations.CheckOut(obj.objVerEx.ObjID);
                FileInfo uploadFileInfo = new FileInfo(filePath);

                obj.objVerEx.Vault.ObjectFileOperations.AddFile(objVersion.ObjVer,
                                                                uploadFileInfo.Name.Replace(uploadFileInfo.Extension, ""),
                                                                uploadFileInfo.Extension.Replace(".", ""),
                                                                uploadFileInfo.FullName);
                obj.objVerEx.Vault.ObjectOperations.CheckIn(objVersion.ObjVer);

                return(new T()
                {
                    objVerEx = obj.objVerEx
                });
            } catch (Exception exception) {
                if (objVersion != null &&
                    obj.objVerEx.Vault.ObjectOperations.IsObjectCheckedOut(objVersion.ObjVer.ObjID, true))
                {
                    obj.objVerEx.Vault.ObjectOperations.CheckIn(objVersion.ObjVer);
                }
                Console.WriteLine($"Exception during object update: {exception.Message}");
                throw;
            }
        }
        public static void Add <T>(this IObjVerEx obj, int propertyId, T?value)
            where T : struct
        {
            if (typeof(T).IsEnum && (value != null && value.GetType() == typeof(T)))
            {
                Lookups lookups = obj.objVerEx.GetLookups(propertyId);

                string value_list_name = typeof(T).Name.Replace("VL", "");
                if (!string.IsNullOrEmpty(value_list_name) && !char.IsDigit(value_list_name[1]))
                {
                    value_list_name = value_list_name.Substring(1, value_list_name.Length - 1);
                }

                int ObjectType = (int)Enum.Parse(typeof(VL_ValueLists), value_list_name, true);

                Lookup lookupToAdd = new Lookup()
                {
                    Item       = Convert.ToInt32(value),
                    ObjectType = ObjectType
                };
                if (lookups.GetLookupIndexByItem(lookupToAdd.Item) == -1)
                {
                    lookups.Add(-1, lookupToAdd);
                }

                PropertyValue propValue = new PropertyValue()
                {
                    PropertyDef = (int)propertyId
                };
                propValue.TypedValue.SetValueToMultiSelectLookup(lookups);
                obj.objVerEx.SetProperty(propValue);
            }
        }
        public static bool HasPermissions(this IObjVerEx obj, PERMISSIONS permissionsToCheck, int userOrGroupId, bool isGroup)
        {
            if (obj.objVerEx.Permissions == null)
            {
                return(false);
            }

            bool hasPermission = true;
            ObjectVersionPermissions   objPermissions = obj.objVerEx.Permissions;
            AccessControlListComponent aclComponent   = objPermissions.AccessControlList.CustomComponent;
            AccessControlEntryData     aceData        = aclComponent.GetACEByUserOrGroupID(userOrGroupId, isGroup);

            if ((permissionsToCheck & PERMISSIONS.READ) == PERMISSIONS.READ)
            {
                hasPermission = hasPermission && aceData.ReadPermission == MFPermission.MFPermissionAllow;
            }
            if ((permissionsToCheck & PERMISSIONS.DELETE) == PERMISSIONS.DELETE)
            {
                hasPermission = hasPermission && aceData.DeletePermission == MFPermission.MFPermissionAllow;
            }
            if ((permissionsToCheck & PERMISSIONS.EDIT) == PERMISSIONS.EDIT)
            {
                hasPermission = hasPermission && aceData.EditPermission == MFPermission.MFPermissionAllow;
            }
            if ((permissionsToCheck & PERMISSIONS.CHANGE_PERMISSIONS) == PERMISSIONS.CHANGE_PERMISSIONS)
            {
                hasPermission = hasPermission && aceData.ChangePermissionsPermission == MFPermission.MFPermissionAllow;
            }
            if ((permissionsToCheck & PERMISSIONS.ATTACH_OBJECT) == PERMISSIONS.ATTACH_OBJECT)
            {
                hasPermission = hasPermission && aceData.AttachObjectsPermission == MFPermission.MFPermissionAllow;
            }

            return(hasPermission);
        }
        public static void SetMultiSelectLookupValueForPropertyDefCache <T>(this IObjVerEx ot, int property_id, IList <T> value) where T : class, IObjVerEx, new()
        {
            ot.RemoveCachedProperty(property_id);
            MultiSelectOTProperty <T> pd_value = new MultiSelectOTProperty <T>(ot, property_id, value);

            ot.CacheProperty(property_id, pd_value);
        }
 private static void undoObjectCheckoutIfCheckedOut(this IObjVerEx obj)
 {
     if (obj.objVerEx.Vault.ObjectOperations.IsObjectCheckedOut(obj.objVerEx.ObjID, true))
     {
         obj.objVerEx.Vault.ObjectOperations.ForceUndoCheckout(obj.objVerEx.ObjVer);
     }
 }
        public static bool objChanged(this IObjVerEx obj, PropertyValues compared_to)
        {
            if (obj.objVerEx.PreviousVersion == null)
            {
                return(true);
            }

            PropertyValues cur  = compared_to;
            PropertyValues prev = obj.objVerEx.Properties;

            if (cur.Count != prev.Count)
            {
                return(true);
            }

            for (int i = 1; i < cur.Count; i++)
            {
                if (cur[i].GetValueAsUnlocalizedText() != prev[i].GetValueAsUnlocalizedText() &&
                    i != 4 &&                        //last modified by list index
                    i != 2 &&                        //last modified date list index
                    i != 5)                          // version date list index
                {
                    return(true);
                }
            }
            return(false);
        }
        public static bool HasChanged(this IObjVerEx obj, int id)
        {
            // first version?
            if (obj.objVerEx.Version == 1)
            {
                return(false);
            }

            if (null == obj.objVerEx.PreviousVersion)
            {
                return(false);
            }

            if (!obj.objVerEx.HasProperty(id) ||
                !obj.objVerEx.PreviousVersion.HasProperty(id))
            {
                return(false);
            }

            PropertyValue curr = obj.objVerEx.GetProperty(id);
            PropertyValue prev = obj.objVerEx.PreviousVersion.GetProperty(id);

            if (curr.IsEqual(prev, EqualityCompareOptions.Default))
            {
                return(false);
            }

            return(true);
        }
 public static void RemoveCachedProperty(this IObjVerEx obj, int property_id)
 {
     if (obj.object_properties_cache.ContainsKey(property_id))
     {
         obj.object_properties_cache.Remove(property_id);
     }
 }
        public static void Add <T>(this IObjVerEx obj, int propertyId, T value)
            where T : IObjVerEx
        {
            if (obj.objVerEx.HasProperty(propertyId))
            {
                PropertyValue propValue = obj.objVerEx.GetProperty(propertyId);
                Lookups       lookups   = propValue.TypedValue.GetValueAsLookups();

                if (lookups == null)
                {
                    lookups = new Lookups();
                }

                Lookup lookupToAdd = new Lookup()
                {
                    Item       = value.objVerEx.ID,
                    ObjectType = value.objVerEx.Type
                };
                if (lookups.GetLookupIndexByItem(lookupToAdd.Item) == -1)
                {
                    lookups.Add(-1, lookupToAdd);
                }

                propValue.TypedValue.SetValueToMultiSelectLookup(lookups);
                obj.objVerEx.SetProperty(propValue);
            }
        }
        public static string GetGenericTitle(this IObjVerEx obj, int property_def_title_id, string format, string old_text, string new_text)
        {
            string title        = null;
            string old_property = null;
            string new_property = null;

            if (!obj.objVerEx.HasProperty(property_def_title_id) || string.IsNullOrWhiteSpace(format))
            {
                return(null);
            }

            title        = obj.objVerEx.GetPropertyText(property_def_title_id);
            old_property = string.IsNullOrWhiteSpace(old_text) ? "" : string.Format(format, old_text);
            new_property = string.IsNullOrWhiteSpace(new_text) ? "" : string.Format(format, new_text);

            if (title.EndsWith(old_property))
            {
                title = title.Replace(old_property, new_property);
            }
            else if (!title.EndsWith(new_property))
            {
                title += new_property;
            }

            return(title);
        }
 public static void undelete(this IObjVerEx obj)
 {
     try {
         ObjectVersion objVer = obj.objVerEx.Vault.ObjectOperations.UndeleteObject(obj.objVerEx.ObjID);
     } catch (Exception exception) {
         throw exception;
     }
 }
 public static void destroy(this IObjVerEx obj)
 {
     try {
         obj.undoObjectCheckoutIfCheckedOut();
         obj.objVerEx.Vault.ObjectOperations.DestroyObject(obj.objVerEx.ObjID, true, -1);
     } catch (Exception exception) {
         throw exception;
     }
 }
 public static void CacheProperty(this IObjVerEx obj, int property_id, object value)
 {
     if (obj.object_properties_cache.ContainsKey(property_id))
     {
         obj.object_properties_cache[property_id] = value;
         return;
     }
     obj.object_properties_cache.Add(property_id, value);
 }
 public static void delete(this IObjVerEx obj)
 {
     try {
         obj.undoObjectCheckoutIfCheckedOut();
         ObjectVersion objVer = obj.objVerEx.Vault.ObjectOperations.DeleteObject(obj.objVerEx.ObjVer.ObjID);
     } catch (Exception exception) {
         throw exception;
     }
 }
 public static void SetNamedACL(this IObjVerEx obj, VL_NACLs?vl_nacl)
 {
     if (vl_nacl == null)
     {
         return;
     }
     SetNamedACL(obj, new NamedACL {
         ID = (int)vl_nacl
     });
 }
        public static void Clear(this IObjVerEx obj, object prop, MFDataType data_type)
        {
            PropertyValue pv = new PropertyValue()
            {
                PropertyDef = (int)prop
            };

            pv.TypedValue.SetValueToNULL(data_type);
            obj.objVerEx.SetProperty(pv);
        }
        public static T GetSingleSelectLookupValueForPropertyDefCache <T>(this IObjVerEx ot, int property_id) where T : class, IObjVerEx, new()
        {
            if (ot.object_properties_cache.ContainsKey(property_id))
            {
                return(ot.object_properties_cache[property_id] as T);
            }
            T pd_value = ot.objVerEx.GetSingleSelectLookupValueForPropertyDef <T>(property_id);

            ot.CacheProperty(property_id, pd_value);
            return(pd_value);
        }
        public static void SetToNull(this IObjVerEx obj, int id, MFDataType data_type)
        {
            obj.RemoveCachedProperty(id);
            PropertyValue pv = new PropertyValue()
            {
                PropertyDef = id
            };

            pv.TypedValue.SetValueToNULL(data_type);
            obj.objVerEx.SetProperty(pv);
        }
        public static bool Contains <T>(this IObjVerEx obj, int propertyId, T value)
            where T : IObjVerEx
        {
            if (obj.objVerEx.HasProperty(propertyId))
            {
                PropertyValue propValue = obj.objVerEx.GetProperty(propertyId);
                Lookups       lookups   = propValue.TypedValue.GetValueAsLookups();
                int           itemIndex = lookups.GetLookupIndexByItem(value.objVerEx.ID);

                return(itemIndex != -1);
            }
            return(false);
        }
        public static bool IsNull(this IObjVerEx obj, int id)
        {
            if (obj.objVerEx.HasProperty(id))
            {
                PropertyValue pv = obj.objVerEx.GetProperty(id);
                if (pv.Value.IsNULL())
                {
                    return(true);
                }
            }

            return(false);
        }
        public static bool Contains <T>(this IObjVerEx obj, int propertyId, T?value)
            where T : struct
        {
            if (typeof(T).IsEnum && (value != null && value.GetType() == typeof(T)))
            {
                PropertyValue propValue = obj.objVerEx.GetProperty(propertyId);
                Lookups       lookups   = obj.objVerEx.GetLookups(propertyId);
                int           itemIndex = lookups.GetLookupIndexByItem(Convert.ToInt32(value));

                return(itemIndex != -1);
            }
            return(false);
        }
        public static int GetVautlValueListItem(this IObjVerEx obj, VL_ValueLists vlId, string item)
        {
            ValueListItems vlItems = obj.objVerEx.Vault.ValueListItemOperations.GetValueListItems((int)vlId);

            foreach (ValueListItem vlItem in vlItems)
            {
                if (item.Equals(vlItem.Name.Trim(), StringComparison.InvariantCultureIgnoreCase))
                {
                    return(vlItem.ID);
                }
            }

            return(-1);
        }
 public static void SetPropertyFrom(this IObjVerEx obj, IObjVerEx source_obj, int property_id, MFDataType property_datatype)
 {
     if (source_obj.objVerEx.HasProperty(property_id))
     {
         obj.RemoveCachedProperty(property_id);
         if (source_obj.IsNull(property_id))
         {
             obj.SetToNull(property_id, property_datatype);
         }
         else
         {
             obj.objVerEx.SetProperty(source_obj.objVerEx.GetProperty(property_id));
         }
     }
 }
        public static void Remove <T>(this IObjVerEx obj, int propertyId, T value)
            where T : IObjVerEx
        {
            if (obj.objVerEx.HasProperty(propertyId))
            {
                PropertyValue propValue = obj.objVerEx.GetProperty(propertyId);
                Lookups       lookups   = propValue.TypedValue.GetValueAsLookups();
                int           itemIndex = lookups.GetLookupIndexByItem(value.objVerEx.ID);

                if (itemIndex != -1)
                {
                    lookups.Remove(itemIndex);
                }
            }
        }
        public static void Remove <T>(this IObjVerEx obj, int propertyId, T?value)
            where T : struct
        {
            if (typeof(T).IsEnum && (value != null && value.GetType() == typeof(T)))
            {
                PropertyValue propValue = obj.objVerEx.GetProperty(propertyId);
                Lookups       lookups   = obj.objVerEx.GetLookups(propertyId);
                int           itemIndex = lookups.GetLookupIndexByItem(Convert.ToInt32(value));

                if (itemIndex != -1)
                {
                    lookups.Remove(itemIndex);
                }
            }
        }
        public static void SetSingleSelectFromMultiSelectProperty(this IObjVerEx obj, int singleselect_property, int multiselect_property)
        {
            Vault vault = obj.objVerEx.Vault;

            if (!obj.objVerEx.HasProperty(multiselect_property))
            {
                throw new Exception("Multi-select property not found.");
            }
            if (!obj.objVerEx.HasProperty(singleselect_property))
            {
                throw new Exception("Single-select property not found.");
            }

            PropertyDefAdmin mpd = vault.PropertyDefOperations.GetPropertyDefAdmin(multiselect_property);

            if (mpd == null)
            {
                throw new Exception("Multi-select property not found.");
            }
            if (!mpd.PropertyDef.BasedOnValueList)
            {
                throw new Exception("Invalid multi-select property parameter.");
            }

            PropertyDefAdmin spd = vault.PropertyDefOperations.GetPropertyDefAdmin(singleselect_property);

            if (spd == null)
            {
                throw new Exception("Single-select property not found.");
            }
            if (!spd.PropertyDef.BasedOnValueList)
            {
                throw new Exception("Invalid single-select property parameter.");
            }

            if (spd.PropertyDef.ValueList != mpd.PropertyDef.ValueList)
            {
                throw new Exception("Target property is not  based on the same multi-select value with the Source.");
            }

            Lookups     mvalues      = obj.objVerEx.GetProperty(multiselect_property).TypedValue.GetValueAsLookups();
            IEnumerator m_enumerator = mvalues.GetEnumerator();

            m_enumerator.MoveNext();
            Lookup first_mvalue = m_enumerator.Current as Lookup;

            obj.objVerEx.SetProperty(singleselect_property, MFDataType.MFDatatypeLookup, first_mvalue?.Item);
        }
        public static string GetGenericTitle(this IObjVerEx obj, string format, int property_def_title_id, int property_def_id)
        {
            string old_property = null;
            string new_property = null;

            if (!obj.objVerEx.HasProperty(property_def_title_id) || !obj.objVerEx.HasProperty(property_def_id))
            {
                return(null);
            }

            old_property = obj.objVerEx.PreviousVersion.GetPropertyText(property_def_id);
            old_property = string.IsNullOrWhiteSpace(old_property) ? "" : string.Format(format, old_property);
            new_property = obj.objVerEx.GetPropertyText(property_def_id);
            new_property = string.IsNullOrWhiteSpace(new_property) ? "" : string.Format(format, new_property);

            return(obj.GetGenericTitle(property_def_title_id, format, old_property, new_property));
        }
        public static void DeleteValueListItem(this IObjVerEx obj, VL_ValueLists vlId, string vlItemNameToDelete)
        {
            if (string.IsNullOrWhiteSpace(vlItemNameToDelete))
            {
                return;
            }

            ValueListItems vlItems = obj.objVerEx.Vault.ValueListItemOperations.GetValueListItems((int)vlId);

            foreach (ValueListItem vlItem in vlItems)
            {
                if (vlItemNameToDelete.Equals(vlItem.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    obj.objVerEx.Vault.ValueListItemOperations.RemoveValueListItem((int)vlId, vlItem.ID);
                    break;
                }
            }
        }