00001 
00066 package com.arcle.rmt.superwaba.model.imp;
00067 
00068 import waba.sys.Settings;
00069 import waba.sys.Convert;
00070 import waba.io.Catalog;
00071 import superwaba.ext.xplat.io.ObjectCatalog;
00072 import superwaba.ext.xplat.io.Storable;
00073 import com.arcle.rmt.superwaba.model.DocumentFactory;
00074 import com.arcle.rmt.superwaba.model.Document;
00075 import com.arcle.rmt.superwaba.model.DocumentManager;
00076 
00077 
00087 public class DocumentManagerImpl extends DocumentManager {
00088     public DocumentManagerImpl() {
00089     }
00090 
00091 
00092     
00093     
00094 
00095     public Document loadDocument(String name) {
00096         ObjectCatalog cat = createObjectCatalog(name, ObjectCatalog.READ_ONLY);
00097 
00098         DocumentImpl doc = (DocumentImpl) createDocument();
00099         GenericMemento memento = doc.createMemento();
00100         cat.registerClass(memento);
00101         boolean loaded = cat.loadObjectAt(memento, 0);
00102         cat.close();
00103         if (loaded) {
00104             doc.setMemento(memento);
00105             return doc;
00106         }
00107 
00108         return null;
00109     }
00110 
00111     public void saveDocument(String name, Document doc) {
00112 
00113         if (!doc.isDeleted()) {
00114             ObjectCatalog cat = createObjectCatalog(name, ObjectCatalog.CREATE);
00115             
00116             GenericMemento memento = ((DocumentImpl)doc).createMemento();
00117             cat.insertObjectAt(memento, 0);
00118             cat.close();
00119         } else {
00120             Catalog cat  = new Catalog(makeCatalogName(name), Catalog.WRITE_ONLY);
00121             if (cat.isOpen()) {
00122                 cat.delete();                 
00123                 cat.close();
00124             }
00125         }
00126 
00127     }
00128 
00129     public boolean deleteDocument(String name) {
00130         ObjectCatalog cat = createObjectCatalog(name, ObjectCatalog.READ_WRITE);
00131         return cat.delete();
00132     }
00133 
00134 
00135     public String[] listDocuments() {
00136         String[] catList = ObjectCatalog.listCatalogs(
00137             Convert.chars2int(getCreatorID()),
00138             Convert.chars2int(DOCUMENT_TYPE)
00139         );
00140         if (catList != null ) {
00141             
00142 
00143 
00144 
00145 
00146             String[] documentList =  new String[catList.length];
00147 
00148             for (int i=0; i<documentList.length; i++) {
00149                 documentList[i] = Convert.tokenizeString(
00150                     catList[i],
00151                     '.'
00152                 )[0];
00153             }
00154             return documentList;
00155         } else {
00156             return null;
00157         }
00158     }
00159 
00160 
00165     public Document createDocument() {
00166         return new DocumentImpl();
00167     }
00168 
00169     
00170     
00171 
00177     protected ObjectCatalog createObjectCatalog(String baseName, int mode) {
00178         String catName = makeCatalogName(baseName);
00179         ObjectCatalog cat = new ObjectCatalog(catName, mode);
00180         cat.resetSearch();
00181         return cat;
00182     }
00183 
00184     
00185     
00186 
00190     protected String getCreatorID() {
00191         String crtr = Settings.appCreatorId;
00192         if (crtr == null) {
00193             crtr = CREATOR_ID;
00194         }
00195         return crtr;
00196     }
00197 
00204     protected String makeCatalogName(String baseName) {
00205         return baseName + "." + getCreatorID() + "." + DOCUMENT_TYPE;
00206     }
00207 
00208 
00209     
00210     
00211 
00212 }