Esempio n. 1
0
        public void UpdateTrustees(int docNumber, TrusteeInfo[] trusteesToSet)
        {
            if(trusteesToSet == null)
                throw new ArgumentNullException("trusteesToSet");
            if(trusteesToSet.Length == 0)
                throw new ArgumentException("trusteesToSet is empty");

            var doc = new PCDDocObjectClass();
            doc.SetDST(Dst);
            doc.SetObjectType(ObjectFormDefaultProfile);
            doc.SetProperty(PropertyTargetLibrary, Library);
            doc.SetProperty(PropertyObjectIdentifier, docNumber);

            int result = doc.FetchTrustees();
            if(result != S_OK || doc.ErrNumber != 0)
                throw new DMApiException(string.Format("Cannot fetch trustees for document# {0}.", docNumber), doc.ErrNumber, doc.ErrDescription);

            // season greetings, DM API developers...
            var trustees = doc.GetTrustees();
            if(doc.ErrNumber != 0)
                throw new DMApiException("GetTrustees failed.", doc.ErrNumber, doc.ErrDescription);

            int size = trustees.GetSize();
            foreach(var t in trusteesToSet) {
                int idx = trustees.GetTrusteeIndex(t.Trustee, (int)t.TrusteeType);
                // funking DM API returns index equal GetSize() if trustee is not found
                if(idx == size) {
                    if(t.AccessRights != AccessRights.NoAccess)
                        trustees.AddTrustee(t.Trustee, (int)t.TrusteeType, (int)t.AccessRights);
                }
                else  // update existing
                    if(t.AccessRights == AccessRights.NoAccess)
                        trustees.DeleteTrustee(idx);
                    else
                        trustees.SetTrusteeRights(idx, (int)t.AccessRights);
            }
            result = doc.SetTrustees(trustees);
            if(result != S_OK || doc.ErrNumber != 0)
                throw new DMApiException("SetTrustees failed.", doc.ErrNumber, doc.ErrDescription);

            result = doc.UpdateTrustees();
            if(result != S_OK || doc.ErrNumber != 0)
                throw new DMApiException(string.Format("UpdateTrustees failed for document# {0}.", docNumber), doc.ErrNumber, doc.ErrDescription);

            result = doc.Update();
            if(result != S_OK || doc.ErrNumber != 0)
                throw new DMApiException(string.Format("Cannot update document# {0}.", docNumber), doc.ErrNumber, doc.ErrDescription);
        }
Esempio n. 2
0
        public void UpdateTrustees(int docNumber, TrusteeInfo[] trusteesToSet)
        {
            if (trusteesToSet == null)
            {
                throw new ArgumentNullException("trusteesToSet");
            }
            if (trusteesToSet.Length == 0)
            {
                throw new ArgumentException("trusteesToSet is empty");
            }

            var doc = new PCDDocObjectClass();

            doc.SetDST(DocumentSecurityToken);
            doc.SetObjectType(ObjectFormDefaultProfile);
            doc.SetProperty(PropertyTargetLibrary, LibraryName);
            doc.SetProperty(PropertyObjectIdentifier, docNumber);

            int result = doc.FetchTrustees();

            if (result != S_OK || doc.ErrNumber != 0)
            {
                throw new DMApiException(string.Format("Cannot fetch trustees for document# {0}.", docNumber), doc.ErrNumber, doc.ErrDescription);
            }

            // season greetings, DM API developers...
            var trustees = doc.GetTrustees();

            if (doc.ErrNumber != 0)
            {
                throw new DMApiException("GetTrustees failed.", doc.ErrNumber, doc.ErrDescription);
            }

            int size = trustees.GetSize();

            foreach (var t in trusteesToSet)
            {
                int idx = trustees.GetTrusteeIndex(t.Trustee, (int)t.TrusteeType);
                // funking DM API returns index equal GetSize() if trustee is not found
                if (idx == size)
                {
                    if (t.AccessRights != AccessRights.NoAccess)
                    {
                        trustees.AddTrustee(t.Trustee, (int)t.TrusteeType, (int)t.AccessRights);
                    }
                }
                else  // update existing
                if (t.AccessRights == AccessRights.NoAccess)
                {
                    trustees.DeleteTrustee(idx);
                }
                else
                {
                    trustees.SetTrusteeRights(idx, (int)t.AccessRights);
                }
            }
            result = doc.SetTrustees(trustees);
            if (result != S_OK || doc.ErrNumber != 0)
            {
                throw new DMApiException("SetTrustees failed.", doc.ErrNumber, doc.ErrDescription);
            }

            result = doc.UpdateTrustees();
            if (result != S_OK || doc.ErrNumber != 0)
            {
                throw new DMApiException($"UpdateTrustees failed for document# {docNumber}.", doc.ErrNumber, doc.ErrDescription);
            }

            result = doc.Update();
            if (result != S_OK || doc.ErrNumber != 0)
            {
                throw new DMApiException($"Cannot update document# {docNumber}.", doc.ErrNumber, doc.ErrDescription);
            }
        }