Changeset 24887


Ignore:
Timestamp:
2011-12-13T09:59:08+13:00 (12 years ago)
Author:
sjm84
Message:

Some updates and formatting changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSDocumentModel.java

    r24409 r24887  
    66import java.io.FileOutputStream;
    77import java.io.FileWriter;
     8import java.io.IOException;
    89import java.nio.channels.FileChannel;
    910import java.util.ArrayList;
     
    4445
    4546    //Error codes
    46     protected static final int NO_ERROR = 0;
    47     protected static final int ERROR_OID_NOT_SPECIFIED = -1;
    48     protected static final int ERROR_COLLECTION_NOT_SPECIFIED = -2;
    49     protected static final int ERROR_SOURCE_DOCUMENT_OR_SECTION_DOES_NOT_EXIST = -3;
    50     protected static final int ERROR_DESTINATION_DOCUMENT_OR_SECTION_DOES_NOT_EXIST = -4;
    51     protected static final int ERROR_DESTINATION_DOCUMENT_OR_SECTION_ALREADY_EXISTS = -5;
    52     protected static final int ERROR_COULD_NOT_DUPLICATE = -6;
    53     protected static final int ERROR_COULD_NOT_MOVE = -7;
    54     protected static final int ERROR_DOC_XML_COULD_NOT_BE_CREATED = -8;
    55     protected static final int ERROR_EXCEPTION_CREATING_DOC_XML_FILE = -9;
    56     protected static final int ERROR_METADATA_NAME_NOT_SPECIFIED = -10;
    57     protected static final int ERROR_METADATA_VALUE_NOT_SPECIFIED = -11;
    58     protected static final int ERROR_COULD_NOT_RETRIEVE_DOC_XML = -12;
    59     protected static final int ERROR_COULD_NOT_WRITE_TO_DOC_XML = -13;
    60     protected static final int ERROR_COULD_NOT_RETRIEVE_SECTION = -14;
    61     protected static final int ERROR_COULD_NOT_OPEN_DATABASE = -15;
    62     protected static final int ERROR_DATA_NOT_FOUND_IN_DATABASE = -16;
    63     protected static final int ERROR_COULD_NOT_DELETE = -16;
    64     protected static final int ERROR_OID_INCORRECT_FORMAT = -17;
    65     protected static final int ERROR_INVALID_MERGE = -18;
    66     protected static final int ERROR_INVALID_METADATA_POSITION = -19;
    67     protected static final int ERROR_INVALID_SPLIT = -20;
    68     protected static final int ERROR_DESTINATION_OID_NOT_SPECIFIED = -21;
    69     protected static final HashMap<Integer, String> _errorMessageMap;
     47    public static final int NO_ERROR = 0;
     48    public static final int ERROR_OID_NOT_SPECIFIED = -1;
     49    public static final int ERROR_COLLECTION_NOT_SPECIFIED = -2;
     50    public static final int ERROR_SOURCE_DOCUMENT_OR_SECTION_DOES_NOT_EXIST = -3;
     51    public static final int ERROR_DESTINATION_DOCUMENT_OR_SECTION_DOES_NOT_EXIST = -4;
     52    public static final int ERROR_DESTINATION_DOCUMENT_OR_SECTION_ALREADY_EXISTS = -5;
     53    public static final int ERROR_COULD_NOT_DUPLICATE = -6;
     54    public static final int ERROR_COULD_NOT_MOVE = -7;
     55    public static final int ERROR_DOC_XML_COULD_NOT_BE_CREATED = -8;
     56    public static final int ERROR_EXCEPTION_CREATING_DOC_XML_FILE = -9;
     57    public static final int ERROR_METADATA_NAME_NOT_SPECIFIED = -10;
     58    public static final int ERROR_METADATA_VALUE_NOT_SPECIFIED = -11;
     59    public static final int ERROR_COULD_NOT_RETRIEVE_DOC_XML = -12;
     60    public static final int ERROR_COULD_NOT_WRITE_TO_DOC_XML = -13;
     61    public static final int ERROR_COULD_NOT_RETRIEVE_SECTION = -14;
     62    public static final int ERROR_COULD_NOT_OPEN_DATABASE = -15;
     63    public static final int ERROR_DATA_NOT_FOUND_IN_DATABASE = -16;
     64    public static final int ERROR_COULD_NOT_DELETE = -16;
     65    public static final int ERROR_OID_INCORRECT_FORMAT = -17;
     66    public static final int ERROR_INVALID_MERGE = -18;
     67    public static final int ERROR_INVALID_METADATA_POSITION = -19;
     68    public static final int ERROR_INVALID_SPLIT = -20;
     69    public static final int ERROR_DESTINATION_OID_NOT_SPECIFIED = -21;
     70    public static final HashMap<Integer, String> _errorMessageMap;
    7071
    7172    static
     
    248249     *            instead.
    249250     */
    250     public void documentDuplicate(String oid, String collection, String newOID, String newCollection, String lang, String uid)
     251    public void documentMoveOrDuplicate(String oid, String collection, String newOID, String newCollection, int operation, boolean move, String lang, String uid)
    251252    {
    252253        if ((_errorStatus = checkOIDandCollection(oid, collection, lang, uid)) != NO_ERROR)
     
    267268            return;
    268269        }
    269         else if (archiveCheckDocumentOrSectionExists(newOID, newCollection, lang, uid))
    270         {
    271             _errorStatus = ERROR_DESTINATION_DOCUMENT_OR_SECTION_ALREADY_EXISTS;
    272             return;
    273         }
    274 
    275         //Check what operation we are performing
    276         int op = getOperation(oid, newOID);
    277270
    278271        boolean requiresDatabaseEntry = false;
    279         switch (op)
     272        int operationType = getOperation(oid, newOID);
     273        switch (operationType)
    280274        {
    281275        case OPERATION_TYPE_DOC_TO_DOC:
     
    302296                }
    303297            }
     298            else
     299            {
     300                _errorStatus = ERROR_COULD_NOT_DUPLICATE;
     301                return;
     302            }
     303
     304            if (move)
     305            {
     306                deleteDirectory(dirToDuplicate);
     307            }
     308
    304309            requiresDatabaseEntry = true;
    305310            break;
     
    316321            }
    317322
    318             documentXMLSetSection(newOID, newCollection, originalSection, OPERATION_REPLACE, lang, uid);
     323            documentXMLSetSection(newOID, newCollection, originalSection, operation, lang, uid);
     324
     325            if (move)
     326            {
     327                String archiveDirStr = archiveGetDocumentFilePath(oid, collection, lang, uid);
     328                if (_errorStatus != NO_ERROR)
     329                {
     330                    return;
     331                }
     332
     333                File archiveDir = new File(archiveDirStr);
     334
     335                if (archiveDir.exists() && archiveDir.isDirectory())
     336                {
     337                    deleteDirectory(archiveDir);
     338                }
     339            }
    319340            break;
    320341        }
     
    336357            }
    337358
    338             documentXMLSetSection(newOID, newCollection, originalSection, OPERATION_REPLACE, lang, uid);
     359            documentXMLSetSection(newOID, newCollection, originalSection, operation, lang, uid);
     360
     361            if (move)
     362            {
     363                originalDocument = getDocXML(oid, collection, lang, uid);
     364                originalSection.getParentNode().removeChild(originalSection);
     365
     366                //Write the new change back into the file
     367                if (!writeXMLFile(originalDocument, oid, collection, lang, uid))
     368                {
     369                    _errorStatus = ERROR_COULD_NOT_WRITE_TO_DOC_XML;
     370                    return;
     371                }
     372            }
    339373
    340374            requiresDatabaseEntry = true;
     
    346380            Element originalSection = getSectionBySectionNumber(originalDocument, getSectionFromOID(oid));
    347381
    348             documentXMLCreateSection(newOID, newCollection, lang, uid);
     382            if (operation == OPERATION_REPLACE)
     383            {
     384                documentXMLCreateSection(newOID, newCollection, lang, uid);
     385                if (_errorStatus != NO_ERROR)
     386                {
     387                    return;
     388                }
     389            }
     390
     391            documentXMLSetSection(newOID, newCollection, originalSection, operation, lang, uid);
    349392            if (_errorStatus != NO_ERROR)
    350393            {
    351394                return;
    352395            }
    353 
    354             documentXMLSetSection(newOID, newCollection, originalSection, OPERATION_REPLACE, lang, uid);
     396           
     397            if (move)
     398            {
     399                originalDocument = getDocXML(oid, collection, lang, uid);
     400                originalSection.getParentNode().removeChild(originalSection);
     401
     402                //Write the new change back into the file
     403                if (!writeXMLFile(originalDocument, oid, collection, lang, uid))
     404                {
     405                    _errorStatus = ERROR_COULD_NOT_WRITE_TO_DOC_XML;
     406                    return;
     407                }
     408            }
     409
    355410            break;
    356411        }
     
    396451        //TODO: Implement
    397452        return null;
    398     }
    399 
    400     /**
    401      * Can be used to move a document or section from one place to another.
    402      *
    403      * @param oid
    404      *            is the identifier of the document/section to move.
    405      * @param collection
    406      *            is the collection the source document resides in.
    407      * @param newOID
    408      *            is the new identifer for the moved document.
    409      * @param newCollection
    410      *            is the collection the new document/section will be moved to.
    411      *            If this is null then the collection parameter will be used
    412      *            instead.
    413      */
    414     public void documentMove(String oid, String collection, String newOID, String newCollection, String lang, String uid)
    415     {
    416         if ((_errorStatus = checkOIDandCollection(oid, collection, lang, uid)) != NO_ERROR)
    417         {
    418             return;
    419         }
    420 
    421         documentDuplicate(oid, collection, newOID, newCollection, lang, uid);
    422         if (_errorStatus != NO_ERROR)
    423         {
    424             return;
    425         }
    426 
    427         documentDelete(oid, collection, lang, uid);
    428453    }
    429454
     
    11411166     *            can be one of OPERATION_REPLACE, OPERATION_INSERT_BEFORE,
    11421167     *            OPERATION_INSERT_AFTER or OPERATION_APPEND.
     1168     * @throws IOException
    11431169     */
    11441170    public void documentXMLSetSection(String oid, String collection, Element newSection, int operation, String lang, String uid)
     
    11721198        }
    11731199
    1174         Element importedSection = (Element) docXML.importNode(newSection, true);
     1200        Element importedSection = (Element) docXML.importNode(newSection.cloneNode(true), true);
     1201        Node sectionParent = existingSection.getParentNode();
    11751202
    11761203        if (operation == OPERATION_APPEND)
     
    11801207        else
    11811208        {
    1182             Node sectionParent = existingSection.getParentNode();
    1183 
    11841209            //Remove the attributes that are only there to help us find the section
    11851210            importedSection.removeAttribute(GSXML.NODE_ID_ATT);
     
    11921217            else if (operation == OPERATION_INSERT_AFTER)
    11931218            {
    1194                 Element sibling = (Element) existingSection.getNextSibling();
    1195                 if (sibling == null)
     1219                Node siblingNode = existingSection.getNextSibling();
     1220                while (siblingNode != null && siblingNode.getNodeType() != Node.ELEMENT_NODE)
    11961221                {
    1197                     sectionParent.insertBefore(importedSection, sibling);
     1222                    siblingNode = siblingNode.getNextSibling();
     1223                }
     1224
     1225                if (siblingNode != null)
     1226                {
     1227                    sectionParent.insertBefore(importedSection, siblingNode);
    11981228                }
    11991229                else
     
    12081238            }
    12091239        }
    1210 
     1240       
    12111241        //Write the new change back into the file
    12121242        if (!writeXMLFile(docXML, oid, collection, lang, uid))
     
    16071637    /**
    16081638     * Gets the list of associated files for a given document.
    1609      * @param oid is the identifier that will be used to search for associated documents.
    1610      * @param collection is the collection whose database will be searched.
     1639     *
     1640     * @param oid
     1641     *            is the identifier that will be used to search for associated
     1642     *            documents.
     1643     * @param collection
     1644     *            is the collection whose database will be searched.
    16111645     * @return the list of associated files.
    16121646     */
     
    19561990
    19571991            NodeList sections = GSXML.getChildrenByTagName(current, GSXML.DOCXML_SECTION_ELEM);
    1958 
    19591992            if (levels.length > 1)
    19601993            {
     
    20032036        }
    20042037
    2005         coll_db.openDatabase(_siteHome + File.separatorChar + "collect" + File.separatorChar + collection + File.separatorChar + "archives" + File.separatorChar + dbName + dbExt, readWrite);
     2038        coll_db.openDatabase(GSFile.collectionArchiveDir(_siteHome, collection) + File.separatorChar + dbName + dbExt, readWrite);
    20062039
    20072040        return coll_db;
    20082041    }
     2042
     2043    public int operationStringToInt(String operation)
     2044    {
     2045        if (operation.equals("insertBefore"))
     2046        {
     2047            return OPERATION_INSERT_BEFORE;
     2048        }
     2049        else if (operation.equals("insertAfter"))
     2050        {
     2051            return OPERATION_INSERT_AFTER;
     2052        }
     2053        else if (operation.equals("append"))
     2054        {
     2055            return OPERATION_APPEND;
     2056        }
     2057        else
     2058        {
     2059            return OPERATION_REPLACE;
     2060        }
     2061    }
     2062   
     2063    public int getErrorStatus()
     2064    {
     2065        return _errorStatus;
     2066    }
    20092067}
Note: See TracChangeset for help on using the changeset viewer.