/// <summary>
 /// AUTHOR: Ashok Kolwal
 /// COMPANY: VITRANA
 /// Version: 1.0
 /// Description: The execute method of an IDfMoveOperation object moves the current versions of documents or folders from one
 /// repository location to an other by unlinking them from the source location and linking them to the destination.
 /// Versions other than the current version remain linked to the original location.
 /// Last Modified date: 11 Jul,2017
 /// </summary>
 /// <param name="mySession"></param>
 /// <param name="docId"></param>
 /// <param name="destination"></param>
 /// <returns></returns>
 public String MoveObject(IDfSession mySession, String docId, String destination)
 {
     try
     {
         IDfId idObj = mySession.getIdByQualification("dm_sysobject where r_object_id='" + docId + "'");
         // Create a new client instance.
         IDfClientX clientx = new DfClientX();
         // Use the factory method to create an IDfCopyOperation instance.
         IDfMoveOperation mo = clientx.getMoveOperation();
         // Create an instance for the destination directory.
         IDfFolder destinationDirectory = mySession.getFolderByPath(destination);
         // Set the destination directory by ID.
         mo.setDestinationFolderId(destinationDirectory.getObjectId());
         // Create a document object that represents the document being copied.
         IDfDocument doc = (IDfDocument)mySession.getObject(idObj);
         // Create a move node, adding the document to the move operation object.
         IDfMoveNode node = (IDfMoveNode)mo.add(doc);
         // Execute and return results
         if (mo.execute())
         {
             return("Move operation successful.");
         }
         else
         {
             return("Move operation failed.");
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error while moving object : " + ex.Message + "\n StackTrace: " + ex.StackTrace);
         return(null);
     }
 }
        /// <summary>
        /// AUTHOR: Ashok Kolwal
        /// COMPANY: VITRANA
        /// Version: 1.0
        /// Description: The execute method of an IDfCopyOperation object copies the current versions of documents
        /// or folders from one repository location to another.
        /// Last Modified date: 11 Jul,2017
        /// </summary>
        /// <param name="sessionManager"></param>
        /// <param name="repositoryName"></param>
        /// <param name="docId"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        public String CopyDocument(IDfSessionManager sessionManager, String repositoryName, String docId, String destination)
        {
            IDfSession    mySession = null;
            StringBuilder sb        = new StringBuilder("");

            try
            {
                mySession = sessionManager.getSession(repositoryName);
                // Get the object ID based on the object ID string.
                IDfId idObj = mySession.getIdByQualification("dm_sysobject where r_object_id='" + docId + "'");
                // Instantiate an object from the ID.
                IDfSysObject sysObj = (IDfSysObject)mySession.getObject(idObj);
                // Create a new client instance.
                IDfClientX clientx = new DfClientX();
                // Use the factory method to create an IDfCopyOperation instance.
                IDfCopyOperation co = clientx.getCopyOperation();
                // Remove the path separator if it exists.
                if (destination.LastIndexOf("/") == destination.Length - 1 || destination.LastIndexOf("\\") == destination.Length - 1)
                {
                    destination = destination.Substring(0, destination.Length - 1);
                }
                // Create an instance for the destination directory.
                IDfFolder destinationDirectory = mySession.getFolderByPath(destination);
                // Set the destination directory by ID.
                co.setDestinationFolderId(destinationDirectory.getObjectId());
                // Create a document object that represents the document being copied.
                //IDfDocument doc = (IDfDocument)mySession.getObject(new DfId(docId));
                IDfDocument doc = (IDfDocument)mySession.getObject(idObj);
                // Create a copy node, adding the document to the copy operation object.
                IDfCopyNode node = (IDfCopyNode)co.add(doc);
                // Execute and return results
                if (co.execute())
                {
                    return("Copy operation successful.");
                }
                else
                {
                    return("Copy operation failed.");
                }
            }
            // Handle any exceptions.
            catch (Exception ex)
            {
                Console.WriteLine("Error while copying object : " + ex.Message + "\n StackTrace: " + ex.StackTrace);
                return("Exception has been thrown: " + ex);
            }
            // Always, always, release the session in the "finally" clause.
            finally
            {
                sessionManager.release(mySession);
            }
        }
        /// <summary>
        /// AUTHOR: Ashok Kolwal
        /// COMPANY: VITRANA
        /// Version: 1.0
        /// Description: The execute method of an IDfCheckoutOperation object checks out the documents in the operation.
        /// The checkout operation:
        /// • Locks the document
        /// • Copies the document to your local disk
        /// • Always creates registry entries to enable DFC to manage the files it creates on the file system
        /// Last Modified date: 11 Jul,2017
        /// </summary>
        /// <param name="sessionManager"></param>
        /// <param name="repositoryName"></param>
        /// <param name="docId"></param>
        /// <returns></returns>
        public String Checkout(IDfSessionManager sessionManager, String repositoryName, String docId)
        {
            StringBuilder result    = new StringBuilder("");
            IDfSession    mySession = null;

            try
            {
                mySession = sessionManager.getSession(repositoryName);
                // Get the object ID based on the object ID string.
                IDfId idObj = mySession.getIdByQualification("dm_sysobject where r_object_id='" + docId + "'");
                // Instantiate an object from the ID.
                IDfSysObject sysObj = (IDfSysObject)mySession.getObject(idObj);
                // Instantiate a client.
                IDfClientX clientx = new DfClientX();
                // Use the factory method to create a checkout operation object.
                IDfCheckoutOperation coOp = clientx.getCheckoutOperation();
                // Set the location where the local copy of the checked out file
                // is stored.
                coOp.setDestinationDirectory("C:\\");
                // Get the document instance using the document ID.
                IDfDocument doc = (IDfDocument)mySession.getObject(idObj);
                // Create the checkout node by adding the document to the checkout
                // operation.
                IDfCheckoutNode coNode = (IDfCheckoutNode)coOp.add(doc);
                // Verify that the node exists.
                if (coNode == null)
                {
                    result.Append("coNode is null");
                }
                // Execute the checkout operation. Return the result.
                if (coOp.execute())
                {
                    result.Append("Successfully checked out file ID: " + docId);
                }
                else
                {
                    result.Append("Checkout failed.");
                }

                return(result.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while checkout : " + ex.Message + "\n StackTrace: " + ex.StackTrace);
                return("Exception hs been thrown: " + ex);
            }
            finally
            {
                sessionManager.release(mySession);
            }
        }
        /// <summary>
        /// AUTHOR: Ashok Kolwal
        /// COMPANY: VITRANA
        /// Version: 1.0
        /// Description: The execute method of an IDfDeleteOperation object removes documents and folders from the repository.
        /// Last Modified date: 11 Jul,2017
        /// </summary>
        /// <param name="sessionManager"></param>
        /// <param name="repositoryName"></param>
        /// <param name="docId"></param>
        /// <param name="versionNo"></param>
        /// <returns></returns>
        public String DeleteDocument(IDfSessionManager sessionManager, String repositoryName, String docId, int versionNo)
        {
            IDfSession mySession = null;

            try
            {
                mySession = sessionManager.getSession(repositoryName);
                // Get the object ID based on the object ID string.
                IDfId idObj = mySession.getIdByQualification("dm_sysobject where r_object_id='" + docId + "'");
                // Instantiate an object from the ID.
                IDfSysObject sysObj = (IDfSysObject)mySession.getObject(idObj);
                // Create a new client instance.
                IDfClientX clientx = new DfClientX();
                // Use the factory method to create an IDfDeleteOperation instance.
                IDfDeleteOperation delo = clientx.getDeleteOperation();
                // Create a document object that represents the document being copied.
                //IDfDocument doc = (IDfDocument)mySession.getObject(new DfId(docId));
                IDfDocument doc = (IDfDocument)mySession.getObject(idObj);
                // Set the deletion policy. You must do this prior to adding nodes to the
                // Delete operation.
                delo.setVersionDeletionPolicy(versionNo);
                // Create a delete node using the factory method.
                IDfDeleteNode node = (IDfDeleteNode)delo.add(doc);
                if (node == null)
                {
                    return("Node is null.");
                }
                // Execute the delete operation and return results.
                if (delo.execute())
                {
                    return("Delete operation succeeded.");
                }
                else
                {
                    return("Delete operation failed");
                }
            }
            // Handle any exceptions.
            catch (Exception ex)
            {
                Console.WriteLine("Error while Deleting document : " + ex.Message + "\n StackTrace: " + ex.StackTrace);
                return("Exception has been thrown: " + ex);
            }
            // Always, always, release the session in the "finally" clause.
            finally
            {
                sessionManager.release(mySession);
            }
        }
        /// <summary>
        /// AUTHOR: Ashok Kolwal
        /// COMPANY: VITRANA
        /// Version: 1.0
        /// Description: The execute method of an IDfCancelCheckoutOperation object cancels the checkout of documents by releasing locks,
        /// deleting local files if appropriate,and removing registry entries. If the operation’s add method receives a virtual
        /// document as an argument, it also adds all of the document’sdescendants(determined by applying the applicable binding rules),
        /// creating a separate operation node for each.
        /// Last Modified date: 11 Jul,2017
        /// </summary>
        /// <param name="sessionManager"></param>
        /// <param name="repositoryName"></param>
        /// <param name="docId"></param>
        /// <returns></returns>
        public String CancelCheckOut(IDfSessionManager sessionManager, String repositoryName, String docId)
        {
            IDfSession mySession = null;

            try
            {
                mySession = sessionManager.getSession(repositoryName);
                // Get the object ID based on the object ID string.
                IDfId idObj = mySession.getIdByQualification("dm_sysobject where r_object_id='" + docId + "'");
                // Instantiate an object from the ID.
                IDfSysObject sysObj = (IDfSysObject)mySession.getObject(idObj);
                // Get a new client instance.
                IDfClientX clientx = new DfClientX();
                // Use the factory method to create a checkout operation object.
                IDfCancelCheckoutOperation cco = clientx.getCancelCheckoutOperation();
                // Instantiate the document object from the ID string.
                IDfDocument doc = (IDfDocument)mySession.getObject(idObj);
                // Indicate whether to keep the local file.
                cco.setKeepLocalFile(true);
                // Create an empty cancel checkout node.
                IDfCancelCheckoutNode node;
                // Populate the cancel checkout node and add it to the cancel checkout
                // operation.
                node = (IDfCancelCheckoutNode)cco.add(doc);
                // Check to see if the node is null - this will not throw an error.
                if (node == null)
                {
                    return("Node is null");
                }
                // Execute the operation and return the result.
                if (!cco.execute())
                {
                    return("Operation failed");
                }
                return("Successfully cancelled checkout of file ID: " + docId);
            }
            // Handle any exceptions.
            catch (Exception ex)
            {
                Console.WriteLine("Error while cancelCheckOut object : " + ex.Message + "\n StackTrace: " + ex.StackTrace);
                return("Exception has been thrown: " + ex);
            }
            // Always, always, release the session in the "finally" clause.
            finally
            {
                sessionManager.release(mySession);
            }
        }
        /// <summary>
        /// AUTHOR: Ashok Kolwal
        /// COMPANY: VITRANA
        /// Version: 1.0
        /// Description: The execute method of an IDfCheckinOperation object checks documents into the repository.
        /// It creates new objects as required, transfers the content to the repository,
        /// and removes local files if appropriate. It checks in existing objects that any of the nodes
        /// refer to(forexample, throughXML links).
        /// Last Modified date: 11 Jul,2017
        /// </summary>
        /// <param name="sessionManager"></param>
        /// <param name="repositoryName"></param>
        /// <param name="docId"></param>
        /// <returns></returns>
        public String Checkin(IDfSessionManager sessionManager, String repositoryName, String docId)
        {
            IDfSession mySession = null;

            try
            {
                mySession = sessionManager.getSession(repositoryName);
                // Get the object ID based on the object ID string.
                IDfId idObj = mySession.getIdByQualification("dm_sysobject where r_object_id='" + docId + "'");
                // Instantiate an object from the ID.
                IDfSysObject sysObj = (IDfSysObject)mySession.getObject(idObj);
                // Instantiate a client.
                IDfClientX clientx = new DfClientX();
                // Use the factory method to create an IDfCheckinOperation instance.
                IDfCheckinOperation cio = clientx.getCheckinOperation();
                // Set the version increment. In this case, the next major version
                // ( version + 1)
                cio.setCheckinVersion(cio.getCheckinVersion() + 1);
                // When updating to the next major version, you need to explicitly
                // set the version label for the new object to "CURRENT".
                cio.setVersionLabels("CURRENT");
                // Create a document object that represents the document being
                // checked in.
                IDfDocument doc = (IDfDocument)mySession.getObject(idObj);
                // Create a checkin node, adding it to the checkin operation.
                IDfCheckinNode node = (IDfCheckinNode)cio.add(doc);
                // Execute the checkin operation and return the result.
                if (!cio.execute())
                {
                    return("Checkin failed.");
                }
                // After the item is created, you can get it immediately using the
                // getNewObjectId method.
                IDfId newId = node.getNewObjectId();
                return("Checkin succeeded - new object ID is: " + newId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while checkin object : " + ex.Message + "\n StackTrace: " + ex.StackTrace);
                return("Checkin failed.");
            }
            finally
            {
                sessionManager.release(mySession);
            }
        }
        /// <summary>
        /// AUTHOR: Ashok Kolwal
        /// COMPANY: VITRANA
        /// Version: 1.0
        /// Description: The IDfSysObject.getContent() command lets you get the contents of a document as a ByteArrayInputStream
        /// Last Modified date: 11 Jul,2017
        /// </summary>
        /// <param name="sessionManager"></param>
        /// <param name="repositoryName"></param>
        /// <param name="objectIdString"></param>
        /// <returns></returns>
        public bool GetContent(IDfSessionManager sessionManager, String repositoryName, String objectIdString, String outputFolderPath)
        {
            IDfSession mySession = null;
            bool       result    = false;

            try
            {
                mySession = sessionManager.getSession(repositoryName);
                // Get the object ID based on the object ID string.
                IDfId idObj = mySession.getIdByQualification("dm_sysobject where r_object_id='" + objectIdString + "'");
                // Instantiate an object from the ID.
                IDfSysObject sysObj = (IDfSysObject)mySession.getObject(idObj);
                // This is file output path plus file name.
                string fileFullPath = string.Concat(outputFolderPath, "\\", sysObj.getObjectName(), ".", sysObj.getContentType());
                java.io.ByteArrayInputStream inputByteStrm = sysObj.getContent();
                java.io.InputStream          inputStrm     = inputByteStrm;
                java.io.OutputStream         outputStrm    = new java.io.FileOutputStream(fileFullPath);
                // Transfer bytes from in to out
                byte[] byteArry = new byte[30720];
                int    len      = 0;
                while ((len = inputStrm.read(byteArry)) > 0)
                {
                    outputStrm.write(byteArry, 0, len);
                }
                inputStrm.close();
                outputStrm.close();
                result = true;
                // Console.WriteLine("Document has been exported with Name: " + Path.GetFileName(fileFullPath));
            }
            // Handle any exceptions.
            catch (Exception ex)
            {
                // Console.WriteLine(ex.Message);
                throw new Exception("[GetContent] Error: " + ex.Message, ex);
            }
            // Always, always, release the session in the "finally" clause.
            finally
            {
                sessionManager.release(mySession);
            }

            return(result);
        }
        /// <summary>
        /// AUTHOR: Ashok Kolwal
        /// COMPANY: VITRANA
        /// Version: 1.0
        /// Description: You can use the IDfSystemObject.destroyAllVersions() method to permanently remove an object from the database.
        /// If you use the IDfPersistentObject.destroy() method, you will destroy only the specific system object
        /// corresponding to the r_object_id your provide. In this example, we use the destroyAllVersions()method,
        /// which destroys not only the system object with the correspondingID but all iterations of the object.
        /// If you attempt to destroy a directory that has children,the method will return an error.
        /// Last Modified date: 11 Jul,2017
        /// </summary>
        /// <param name="sessionManager"></param>
        /// <param name="repositoryName"></param>
        /// <param name="objectIdString"></param>
        /// <returns></returns>
        public Boolean DestroyObject(IDfSessionManager sessionManager, String repositoryName, String objectIdString)
        {
            IDfSession mySession = null;

            try
            {
                mySession = sessionManager.getSession(repositoryName);
                IDfId        idObj  = mySession.getIdByQualification("dm_sysobject where r_object_id='" + objectIdString + "'");
                IDfSysObject sysObj = (IDfSysObject)mySession.getObject(idObj);
                sysObj.destroyAllVersions();
                // Console.WriteLine("Object has been deleted successfully");
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while Deleting object : " + ex.Message + "\n StackTrace: " + ex.StackTrace);
                return(false);
            }
            finally
            {
                sessionManager.release(mySession);
            }
        }