/// <summary>
        /// Locks and creates neccessary files for editing of document created from selected template document, by using template document data.
        /// </summary>
        /// <param name="document">Document created from specific document template, for that neccessary files for editing have to be craeted in checkout directory.</param>
        /// <param name="template">Template document, that contains data, which have to be used to create neccessary files in checkout directory for editing of specific document.</param>
        /// <returns>
        /// Returns a reference to document with a checkedout file, which can be used to edit a document created from a selected document template.
        /// </returns>
        public virtual FWDocument ApplyDocumentTemplate(FWDocument document, FWDocument template)
        {
            bool existsDocument         = (document != null);
            bool existsTemplateDocument = (template != null && !string.IsNullOrEmpty(template.File) && File.Exists(template.File));

            if (existsDocument && existsTemplateDocument)
            {
                document.Refresh();

                // Dokumenteintrag im Archiv sperren
                if (document.LockId == -1)
                {
                    document = Conn.Content.LockDocument(document.Id);
                }

                string fileExtension               = Path.GetExtension(template.File);
                string checkedOutFilePath          = MakeCheckOutFilePath(document.Name, fileExtension, document.Id.ToString(), document.Version.Id.ToString());
                bool   existsCheckedOutFileAlready = (!string.IsNullOrEmpty(checkedOutFilePath) && File.Exists(checkedOutFilePath));
                if (!existsCheckedOutFileAlready)
                {
                    File.Copy(template.File, checkedOutFilePath);
                    FileInfo fInfo = new FileInfo(checkedOutFilePath);
                    if (fInfo.Exists)
                    {
                        fInfo.Attributes = FileAttributes.Normal;
                        Conn.Content.DownloadManager.CreateStateFile(checkedOutFilePath, document.Id.ToString(), document.Version.Id.ToString(), document.LockId.ToString());
                    }
                }
            }

            return(document);
        }
        /// <summary>
        /// Checks out a selected document and returns back result of checkout. While checkout of selected document checked out and state file will be created in
        /// checkout directory.
        /// </summary>
        /// <param name="document">Document, that have to be checked out.</param>
        /// <returns>
        /// Result of checkout operation.
        /// </returns>
        public CheckoutResult Checkout(FWDocument document)
        {
            CheckoutResult checkOutResult = new CheckoutResult()
            {
                FWDocument = document, Success = false
            };

            try
            {
                bool existsDocForCheckOut = (document != null);
                bool existsDocUrl         = (document != null && document.Version != null && !string.IsNullOrEmpty(document.Version.Url));
                bool existsIxConn         = (document.Conn != null);

                if (existsDocForCheckOut && existsDocUrl && existsIxConn)
                {
                    // Delete not more checked out documents in checkout directory
                    ForceCleanup();

                    // Actualize data of document
                    document.Refresh();
                    FWConnection ixConn = document.Conn;

                    // Exists checkout file already
                    bool existsCheckedOutFile = (!string.IsNullOrEmpty(document.CheckedoutFile) && File.Exists(document.CheckedoutFile));

                    // If checkout file already exists
                    if (existsCheckedOutFile)
                    {
                        // If document lock, not exists for currently user set one
                        if (document.LockId == -1)
                        {
                            FWDocument lockedDocument = ixConn.Content.LockDocument(document.Id);
                            if (lockedDocument.LockId == ixConn.Session.User.id)
                            {
                                checkOutResult.FWDocument = lockedDocument;
                                checkOutResult.Success    = true;
                            }
                            else
                            {
                                checkOutResult.Message = string.Format("Das Dokument {0} kann nicht zur Bearbeitung ausgecheckt werden, da das Sperren des Dokuments für die Bearbeitung nicht erfolgreich durchgeführt werden konnte.", document.Name);
                            }
                        }
                        else
                        {
                            if (document.LockId == ixConn.Session.User.id)
                            {
                                checkOutResult.FWDocument = document;
                                checkOutResult.Success    = true;
                            }
                            else
                            {
                                checkOutResult.Message = string.Format("Das Dokument {0} kann nicht zur Bearbeitung ausgecheckt werden, da dieses bereits durch einen anderen Benutzer für die Bearbeitung gesperrt worden ist.", document.Name);
                            }
                        }
                    }
                    else
                    {
                        if (document.LockId == -1)
                        {
                            FWDocument lockedDocument = ixConn.Content.LockDocument(document.Id);
                            if (lockedDocument.LockId == ixConn.Session.User.id)
                            {
                                // Create checkout file and state file for checked out document
                                CreateCheckedOutDocumentFiles(lockedDocument);

                                lockedDocument.Refresh();

                                checkOutResult.FWDocument = lockedDocument;
                                checkOutResult.Success    = true;
                            }
                            else
                            {
                                checkOutResult.Message = string.Format("Das Dokument {0} kann nicht zur Bearbeitung ausgecheckt werden, da das Sperren des Dokuments für die Bearbeitung nicht erfolgreich durchgeführt werden konnte.", document.Name);
                            }
                        }
                        else if (document.LockId == ixConn.Session.User.id)
                        {
                            // Create checkout file and state file for checked out document
                            CreateCheckedOutDocumentFiles(document);

                            document.Refresh();

                            checkOutResult.FWDocument = document;
                            checkOutResult.Success    = true;
                        }
                        else
                        {
                            checkOutResult.Message = string.Format("Das Dokument {0} kann nicht zur Bearbeitung ausgecheckt werden, da es durch einen anderen Benutzer bereits für die Bearbeitung gesperrt worden ist.", document.Name);
                        }
                    }
                }
                else
                {
                    checkOutResult.Message = "Das Dokument kann nicht zur Bearbeitung ausgecheckt werden. Mögliche Ursachen: Dokument existiert nicht, keine Verbindung zum Indexserver oder die Url des Dokuments ist nicht vorhanden.";
                }
            }
            catch (Exception ex)
            {
                checkOutResult.Success = false;
                checkOutResult.Message = "Das Auschecken des Dokuments zur Bearbeitung ist aufgrund des folgenden Fehlers fehlgeschlagen. Fehler: " + ex.Message;
            }

            return(checkOutResult);
        }