Exemplo n.º 1
0
 public static void Setup_GetByDocId(this  Mock<IWdDocumentRepository> repo, WdDocument doc, bool isLast = true)
 {
     repo.Setup(p => p.GetByDocId(It.IsAny<int>(), doc.DocId, doc.VersionNumber)).Returns(doc);
     if (isLast)
     {
         repo.Setup(p => p.GetByDocId(It.IsAny<int>(), doc.DocId, WdDocumentRepository.LATEST_VERSION)).Returns(doc);
     }
 }
Exemplo n.º 2
0
 void CreateWdDoc(tagWSDOCUMENT wsDoc, ref WdDocument wdDoc)
 {
     Logger.LogTrace(string.Format("Create wdDocument from wsDocument {0}", wsDoc.bstrDescription));
     wdDoc = wdDoc ?? new WdDocument();
     wdDoc.DocId = string.IsNullOrEmpty(wsDoc.bstrDocumentID) ? "" : WsDocumentID.GetWdDocID(wsDoc.bstrDocumentID);
     wdDoc.Ext = string.IsNullOrEmpty(wsDoc.bstrExtension) ? "" : wsDoc.bstrExtension;
     wdDoc.FIleName = string.IsNullOrEmpty(wsDoc.bstrDescription) ? "" : Path.GetFileName(wsDoc.bstrDescription);
     wdDoc.LocalFilePath = string.IsNullOrEmpty(wsDoc.bstrLocalFile) ? "" : wsDoc.bstrLocalFile;
 }
Exemplo n.º 3
0
 void UpdateWsDocData(ref tagWSDOCUMENT wsDoc, WdDocument wdDoc)
 {
     Logger.LogTrace(string.Format("Update Ws local data from wdDocument. {0}", wdDoc.AsString()));
     wsDoc.bstrDocumentID = WsDocumentID.Create(wdDoc.DocId, wdDoc.VersionNumber);
     wsDoc.bstrDescription = wdDoc.FIleName;
     wsDoc.bstrExtension = wdDoc.Ext;
     wsDoc.bstrType = FileUtils.GetFileTypeFromExt(wdDoc.Ext);
     wsDoc.dModifiedTime = wdDoc.ModifiedTime;
     wsDoc.bstrAuthor = wdDoc.Author;
     wsDoc.bstrClass = wdDoc.SecurityClassName;
     wsDoc.bstrOperator = wdDoc.Operator;
 }
        string SimulateSelectionOfFile(DocumentProvider dp, WdDocument wdDocument, string filters)
        {
            var module = dp.module;
            //user selects file           
            Mock.Get(module.Resolve<IOpenFileDialog>()).Setup(a => a.ShowDialog()).Returns(true);
            Mock.Get(module.Resolve<IOpenFileDialog>()).Setup(a => a.SelectedFile).Returns(wdDocument);
            string selected;
            dp.SelectDocumentEx(123, filters, string.Empty, out selected).Assert_Is_S_OK();
            Assert.That(selected, Is.EqualTo(WsDocumentID.Create(wdDocument)));

            //professional asks about selected document
            tagWSDOCUMENT doc1;
            dp.GetDocument(selected, (int)wsGetDocFlags.DF_INFO_ONLY, out doc1).Assert_Is_S_OK();
            Assert.That(doc1.bstrDocumentID, Is.EqualTo(selected));
            Assert.That(doc1.bstrExtension, Is.EqualTo(wdDocument.Ext));
            Assert.That(doc1.bstrDescription, Is.EqualTo(wdDocument.FIleName));
            Assert.IsNotNullOrEmpty(doc1.bstrType);
            Assert.That(doc1.bstrLocalFile, (Is.EqualTo(string.Empty) | Is.Null));

            dp.CloseDocument(ref doc1, 0).Assert_Is_S_OK();

            return doc1.bstrDocumentID;
        }
        tagWSDOCUMENT[] SimulateComparison(DocumentProvider dp, WdDocument wdDoc1, WdDocument wdDoc2)
        {
            var res = new List<tagWSDOCUMENT>();
            foreach (var wdDoc in new[] { wdDoc1, wdDoc2 })
            {
                tagWSDOCUMENT doc;
                dp.GetDocument(WsDocumentID.Create(wdDoc), 0, out doc).Assert_Is_S_OK();
                Assert.That(doc.bstrDocumentID, Is.EqualTo(WsDocumentID.Create(wdDoc)));
                Assert.That(doc.bstrExtension, Is.EqualTo(wdDoc.Ext));
                Assert.That(doc.bstrDescription, Is.EqualTo(wdDoc.FIleName));
                Assert.IsNotNullOrEmpty(doc.bstrType);
                Assert.IsNotNullOrEmpty(doc.bstrLocalFile);

                string descr;
                dp.GetDocIDDescription(WsDocumentID.Create(wdDoc), out descr).Assert_Is_S_OK();

                Assert.That(descr, Is.EqualTo(wdDoc.FIleName));

                res.Add(doc);
            }


            return res.ToArray();
        }
Exemplo n.º 6
0
        public string Add(int sessionhandle, WdDocument wdDoc, string wdBaseDocumentPath)
        {
            Logger.LogTrace(string.Format("Add document {0} to repository start. SessionHandle={0}; wdBaseDocumentPath={1}", wdDoc.DocId, sessionhandle, wdBaseDocumentPath)); 
            var sbNewDocumentId = new StringBuilder(MAX_STR);
            const int hwndParent = 0;
            const string lpStruct = "";
            const string filenameTap = "";
            const int dwReserved = 0;
            const int dwRecNum = 0;

            var rcResult = _wdApi.WDAPI_GetAutoName(sessionhandle, WORLDOX.WDAPI_GNA_FLAG_NONE, string.Empty, sbNewDocumentId, MAX_STR);
            if (rcResult != WORLDOX.WDAPI_RC_OK)
            {
                throw new WdException(rcResult, "Failed to get the new autoname", "WDAPI_GetAutoName");
            }

            var pathTo = string.Format("{0}{1}", Path.GetDirectoryName(wdBaseDocumentPath), Path.DirectorySeparatorChar);
            var newDocId = Path.ChangeExtension(sbNewDocumentId.ToString(), wdDoc.Ext).ToUpperInvariant();

            rcResult = _wdApi.WDAPI_RenameFile(sessionhandle, hwndParent, WORLDOX.WDAPI_RF_FLAG_COPYNOVERS, lpStruct,
                wdBaseDocumentPath, filenameTap, pathTo, newDocId, sbNewDocumentId, MAX_STR, dwReserved);
            switch (rcResult)
            {
                case WORLDOX.WDAPI_RC_OK:
                {
                    break;
                };
                case WORLDOX.WDAPI_RC_FRFILEINVALID:
                case WORLDOX.WDAPI_RC_FRPATHINVALID:
                    throw new WdFileNotFoundException(wdBaseDocumentPath, rcResult, "WDAPI_RenameFile");
                case WORLDOX.WDAPI_RC_FRFILEBUSY:
                    throw new WdFileIsBusyException(wdBaseDocumentPath, rcResult, "WDAPI_RenameFile");
                case WORLDOX.WDAPI_RC_TOFILEBUSY:
                    throw new WdFileIsBusyException(Path.Combine(pathTo, newDocId), rcResult, "WDAPI_RenameFile");
                default:
                {
                    throw new WdException(rcResult, string.Format("Failed to Add document {0} to Worldox", wdDoc.FIleName), "WDAPI_RenameFile");
                }
            }
            try
            {
                File.Copy(wdDoc.LocalFilePath, sbNewDocumentId.ToString(), true);
            }
            catch (UnauthorizedAccessException ex)
            {
                throw new WdFileIsBusyException(ex);
            }
            catch (IOException ex)
            {
                throw new WdException(ex);
            }

            var res = _wdApi.WDAPI_SetProfileField(sbNewDocumentId.ToString(), string.Empty, dwRecNum, WORLDOX.WDAPI_GF_FILE_XNAME, wdDoc.FIleName);
            if (res == 0) // fail (see wdApi)
            {
                throw new WdException(rcResult, string.Format("Failed to change description for document {0}", sbNewDocumentId), "WDAPI_SetProfileField");
            }
            Logger.LogTrace(string.Format("Document {0} added to repository. Path to file: {1}", wdDoc.DocId, sbNewDocumentId));
            return sbNewDocumentId.ToString();
        }
Exemplo n.º 7
0
        public WdDocument GetByPath(int sessionhandle, string path)
        {
            Logger.LogTrace(string.Format("Try to get file from WD. SessionHandle={0}. Path={1}", sessionhandle, path));
            var sbDocId = new StringBuilder(MAX_STR);
            var sbrealName = new StringBuilder(MAX_STR);
            var sbExt = new StringBuilder(MAX_STR);
            var sbFilePath = new StringBuilder(MAX_STR);
            var sbVersionNumber = new StringBuilder(MAX_STR);
            var sbUpdated = new StringBuilder(MAX_STR);
            var sbOwner = new StringBuilder(MAX_STR);
            var sbClassName = new StringBuilder(MAX_STR);
            var sbprofileName = new StringBuilder(MAX_STR);
            var res = 0;

            foreach (var p in new[] 
                      {
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_FILENAME, sbDocId, true),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_EXTENSION, sbExt, true),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_XNAME, sbrealName, false),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_PATHFILE, sbFilePath, true),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_VERSION, sbVersionNumber, true),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_OWNER, sbOwner, false),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_UPDATED, sbUpdated, false),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_CLASSNAME, sbClassName, false),
                new Tuple<int, StringBuilder, bool>(WORLDOX.WDAPI_GF_FILE_GROUP, sbprofileName, false),
            })
            {
                res = _wdApi.WDAPI_GetProfileField(path, null, res, p.Item1, p.Item2, p.Item2.Capacity);
                if (res == 0)
                {
                    if (p.Item3) //cannot get field and field is critical
                    {
                        throw new WdFileNotFoundException(path,
                            string.Format("GetProfileField|Cannot get Field={0} for Document={1}", p.Item1, path), res, "WDAPI_GetProfileField");
                    }
                    else
                    {
                        Logger.LogError(string.Format("Cannot get Field={0} for Document={1}. ErrorCode={2}. Field Skipped as not important.", p.Item1,
                            path, res));
                    }
 
                }
                
            }
            var sVersionNumber = sbVersionNumber.ToString();
            DateTime dtUpdated;
            if (!DateTime.TryParse(sbUpdated.ToString(), out dtUpdated))
            {
                dtUpdated = DateTime.MinValue;
                Logger.LogError(string.Format("Cannot parse datetime = {0}", sbUpdated.ToString()));
            }

            bool isLocked = false;
            bool isLockedByCurrentUser = true;

            IsCheckedOut(sessionhandle, sbDocId.ToString(),out isLocked, out isLockedByCurrentUser);

            var result= new WdDocument
            {
                DocId = sbDocId.ToString(),
                FIleName = sbrealName.ToString(),
                LocalFilePath = sbFilePath.ToString(),
                VersionNumber = string.IsNullOrEmpty(sVersionNumber) ? LATEST_VERSION : Convert.ToInt32(sVersionNumber),
                Ext = sbExt.ToString().Trim('.'),
                IsLocked = isLocked,
                IsLockedByCurrentUser = isLockedByCurrentUser,
                Owner = sbOwner.ToString(),
                ModifiedTime = dtUpdated,
                Author = sbOwner.ToString(),
                SecurityClassName=sbClassName.ToString(),
                FromLFS = string.Equals(sbprofileName.ToString().ToLowerInvariant(), "<no profile>"),
                Operator = "Operator"
            };

            Logger.LogTrace(string.Format("Found document : {0}", result.AsString()));

            return result;
        }
        tagWSDOCUMENT SimulateSaving(DocumentProvider dp, WdDocument newWdDocument,string filters, int filterIndex)
        {
            Mock.Get(dp.module.Resolve<ISaveFileDialog>()).Setup(p => p.ShowDialog()).Returns(true);
            Mock.Get(dp.module.Resolve<ISaveFileDialog>()).Setup(p => p.FilePath).Returns(newWdDocument.LocalFilePath);
            Mock.Get(dp.module.Resolve<ISaveFileDialog>()).Setup(p => p.FormatIndex).Returns(filterIndex);

            int index = 0;
            var wsDoc = new tagWSDOCUMENT();
            int ret;
            dp.GetSaveInfoEx(123, filters, 0, 0, ref index, ref wsDoc, out ret).Assert_Is_S_OK();
            wsDoc.bstrLocalFile = Path.GetTempFileName();
            dp.SaveDocument(ref wsDoc, 0).Assert_Is_S_OK();

            return wsDoc;
        }
        tagWSDOCUMENT SimulateOpeningOfLastVersion(DocumentProvider dp, WdDocument wdDoc)
        {
            var res = new List<tagWSDOCUMENT>();

            IntPtr ptr=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
            try
            {
                int size;
                dp.GetVersions(WsDocumentID.Create(wdDoc), out size, ptr).Assert_Is_S_OK();

                var collectionPtr=MarshallingHelper.GetCollectionFromPtr<IntPtr>(ptr,1).First();
                try
                {
                    var versions = MarshallingHelper.GetCollectionFromPtr<tagWSDOCUMENTVERSION>(collectionPtr, size);

                    var lastVersionDoc = versions.OrderByDescending(p => p.szVersionLabel).First().wsdDocumentDetails;

                    dp.OpenDocument(ref lastVersionDoc).Assert_Is_S_OK();

                    Mock.Get(dp.module.Resolve<IProcessLauncher>()).Verify(a => a.Launch(wdDoc.LocalFilePath));

                    return lastVersionDoc;
                }
                finally
                {
                    Marshal.FreeHGlobal(collectionPtr);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
Exemplo n.º 10
0
 internal void UpdateCache(string szDocumentID, WdDocument wdDoc, tagWSDOCUMENT wsDoc)
 {
     UpdateL1Cache(szDocumentID.ToUpper(), wdDoc, wsDoc);
     UpdateL2Cach(szDocumentID.ToUpper(), wdDoc.LocalFilePath);
 }
Exemplo n.º 11
0
 private void UpdateL1Cache(string szDocumentID, WdDocument wdDoc, tagWSDOCUMENT wsDoc)
 {
     Logger.LogTrace(string.Format("WsDoc added to L1 cache. WsDocId={0}. wsDesc={1}. WdDocId={2}. WdDocLocalPath={3}", szDocumentID,wsDoc.bstrDescription,wdDoc.DocId,wdDoc.LocalFilePath));
     m_docProviderCache[szDocumentID] = new CacheEntity()
     {
         wsDoc = wsDoc,
         wsPath = wdDoc.LocalFilePath
     };
 }
Exemplo n.º 12
0
        public static WdDocument GetDefaultDoc(string filename="document.doc", int versionNumber=1)
        {
            var doc =  new WdDocument
            {
                DocId = filename.ToUpperInvariant(),
                FIleName = filename,
                LocalFilePath = Path.GetTempFileName(),
            
                VersionNumber = versionNumber,
                Ext =Path.GetExtension(filename).TrimStart('.'),
                Owner = "Demo, Worldox(DEMO)"
            };

            File.Delete(doc.LocalFilePath);
            return doc;
        }
Exemplo n.º 13
0
 public static void Setup_GetByPath(this  Mock<IWdDocumentRepository> repo, WdDocument doc)
 {
     repo.Setup(p => p.GetByPath(It.IsAny<int>(), doc.LocalFilePath)).Returns(doc);
 }
Exemplo n.º 14
0
        internal int RelateDocument(ref tagWSDOCUMENT pNewDocument, string existingDocumentId)
        {
            Logger.LogTrace(string.Format("RelateDocument existingDocumentID={0} pNewDocument.bstrDocumentID={1} pNewDocument.bstrLocalFile={2} ", existingDocumentId, pNewDocument.bstrDocumentID, pNewDocument.bstrLocalFile));
            try
            {
                using (var session = _sessionManager.Create())
                {
                    var baseWdDocument = GetWdDocumentByWsId(session.Handle, existingDocumentId);
                    var wdDoc = new WdDocument();
                    CreateWdDoc(pNewDocument, ref wdDoc);
                    var newDocumentFilePath = _wdDocRepo.Add(session.Handle, wdDoc, baseWdDocument.LocalFilePath);
                    var newWdDocument = _wdDocRepo.GetByPath(session.Handle, newDocumentFilePath);
                    
                    _dpCache.UpdateCache(WsDocumentID.Create(newWdDocument), newDocumentFilePath);

                    _wdDocRepo.RelateDocument(session.Handle, baseWdDocument.LocalFilePath, newDocumentFilePath);
                    return DP_HRESULTS.S_OK;
                }
            }
            catch (WdFileNotFoundException ex)
            {
                throw new DPFileNotFoundException(existingDocumentId, ex);
            }
        }
Exemplo n.º 15
0
 void CreateWsDoc(out tagWSDOCUMENT wsDoc, WdDocument wdDoc)
 {
     wsDoc = new tagWSDOCUMENT();
     UpdateWsDocData(ref wsDoc, wdDoc);
 }
Exemplo n.º 16
0
 public static string Create(WdDocument doc)
 {
     return Create(doc.DocId, doc.VersionNumber);
 }