Changeset 24887
- Timestamp:
- 2011-12-13T09:59:08+13:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSDocumentModel.java
r24409 r24887 6 6 import java.io.FileOutputStream; 7 7 import java.io.FileWriter; 8 import java.io.IOException; 8 9 import java.nio.channels.FileChannel; 9 10 import java.util.ArrayList; … … 44 45 45 46 //Error codes 46 p rotectedstatic final int NO_ERROR = 0;47 p rotectedstatic final int ERROR_OID_NOT_SPECIFIED = -1;48 p rotectedstatic final int ERROR_COLLECTION_NOT_SPECIFIED = -2;49 p rotectedstatic final int ERROR_SOURCE_DOCUMENT_OR_SECTION_DOES_NOT_EXIST = -3;50 p rotectedstatic final int ERROR_DESTINATION_DOCUMENT_OR_SECTION_DOES_NOT_EXIST = -4;51 p rotectedstatic final int ERROR_DESTINATION_DOCUMENT_OR_SECTION_ALREADY_EXISTS = -5;52 p rotectedstatic final int ERROR_COULD_NOT_DUPLICATE = -6;53 p rotectedstatic final int ERROR_COULD_NOT_MOVE = -7;54 p rotectedstatic final int ERROR_DOC_XML_COULD_NOT_BE_CREATED = -8;55 p rotectedstatic final int ERROR_EXCEPTION_CREATING_DOC_XML_FILE = -9;56 p rotectedstatic final int ERROR_METADATA_NAME_NOT_SPECIFIED = -10;57 p rotectedstatic final int ERROR_METADATA_VALUE_NOT_SPECIFIED = -11;58 p rotectedstatic final int ERROR_COULD_NOT_RETRIEVE_DOC_XML = -12;59 p rotectedstatic final int ERROR_COULD_NOT_WRITE_TO_DOC_XML = -13;60 p rotectedstatic final int ERROR_COULD_NOT_RETRIEVE_SECTION = -14;61 p rotectedstatic final int ERROR_COULD_NOT_OPEN_DATABASE = -15;62 p rotectedstatic final int ERROR_DATA_NOT_FOUND_IN_DATABASE = -16;63 p rotectedstatic final int ERROR_COULD_NOT_DELETE = -16;64 p rotectedstatic final int ERROR_OID_INCORRECT_FORMAT = -17;65 p rotectedstatic final int ERROR_INVALID_MERGE = -18;66 p rotectedstatic final int ERROR_INVALID_METADATA_POSITION = -19;67 p rotectedstatic final int ERROR_INVALID_SPLIT = -20;68 p rotectedstatic final int ERROR_DESTINATION_OID_NOT_SPECIFIED = -21;69 p rotectedstatic 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; 70 71 71 72 static … … 248 249 * instead. 249 250 */ 250 public void document Duplicate(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) 251 252 { 252 253 if ((_errorStatus = checkOIDandCollection(oid, collection, lang, uid)) != NO_ERROR) … … 267 268 return; 268 269 } 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 performing276 int op = getOperation(oid, newOID);277 270 278 271 boolean requiresDatabaseEntry = false; 279 switch (op) 272 int operationType = getOperation(oid, newOID); 273 switch (operationType) 280 274 { 281 275 case OPERATION_TYPE_DOC_TO_DOC: … … 302 296 } 303 297 } 298 else 299 { 300 _errorStatus = ERROR_COULD_NOT_DUPLICATE; 301 return; 302 } 303 304 if (move) 305 { 306 deleteDirectory(dirToDuplicate); 307 } 308 304 309 requiresDatabaseEntry = true; 305 310 break; … … 316 321 } 317 322 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 } 319 340 break; 320 341 } … … 336 357 } 337 358 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 } 339 373 340 374 requiresDatabaseEntry = true; … … 346 380 Element originalSection = getSectionBySectionNumber(originalDocument, getSectionFromOID(oid)); 347 381 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); 349 392 if (_errorStatus != NO_ERROR) 350 393 { 351 394 return; 352 395 } 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 355 410 break; 356 411 } … … 396 451 //TODO: Implement 397 452 return null; 398 }399 400 /**401 * Can be used to move a document or section from one place to another.402 *403 * @param oid404 * is the identifier of the document/section to move.405 * @param collection406 * is the collection the source document resides in.407 * @param newOID408 * is the new identifer for the moved document.409 * @param newCollection410 * is the collection the new document/section will be moved to.411 * If this is null then the collection parameter will be used412 * 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);428 453 } 429 454 … … 1141 1166 * can be one of OPERATION_REPLACE, OPERATION_INSERT_BEFORE, 1142 1167 * OPERATION_INSERT_AFTER or OPERATION_APPEND. 1168 * @throws IOException 1143 1169 */ 1144 1170 public void documentXMLSetSection(String oid, String collection, Element newSection, int operation, String lang, String uid) … … 1172 1198 } 1173 1199 1174 Element importedSection = (Element) docXML.importNode(newSection, true); 1200 Element importedSection = (Element) docXML.importNode(newSection.cloneNode(true), true); 1201 Node sectionParent = existingSection.getParentNode(); 1175 1202 1176 1203 if (operation == OPERATION_APPEND) … … 1180 1207 else 1181 1208 { 1182 Node sectionParent = existingSection.getParentNode();1183 1184 1209 //Remove the attributes that are only there to help us find the section 1185 1210 importedSection.removeAttribute(GSXML.NODE_ID_ATT); … … 1192 1217 else if (operation == OPERATION_INSERT_AFTER) 1193 1218 { 1194 Element sibling = (Element)existingSection.getNextSibling();1195 if (sibling == null)1219 Node siblingNode = existingSection.getNextSibling(); 1220 while (siblingNode != null && siblingNode.getNodeType() != Node.ELEMENT_NODE) 1196 1221 { 1197 sectionParent.insertBefore(importedSection, sibling); 1222 siblingNode = siblingNode.getNextSibling(); 1223 } 1224 1225 if (siblingNode != null) 1226 { 1227 sectionParent.insertBefore(importedSection, siblingNode); 1198 1228 } 1199 1229 else … … 1208 1238 } 1209 1239 } 1210 1240 1211 1241 //Write the new change back into the file 1212 1242 if (!writeXMLFile(docXML, oid, collection, lang, uid)) … … 1607 1637 /** 1608 1638 * 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. 1611 1645 * @return the list of associated files. 1612 1646 */ … … 1956 1990 1957 1991 NodeList sections = GSXML.getChildrenByTagName(current, GSXML.DOCXML_SECTION_ELEM); 1958 1959 1992 if (levels.length > 1) 1960 1993 { … … 2003 2036 } 2004 2037 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); 2006 2039 2007 2040 return coll_db; 2008 2041 } 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 } 2009 2067 }
Note:
See TracChangeset
for help on using the changeset viewer.