1 3 package org.contineo.documan; 4 5 import java.io.File ; 6 import java.io.InputStream ; 7 8 import org.contineo.admin.Menu; 9 import org.contineo.admin.dao.MenuDAO; 10 import org.contineo.core.DateBean; 11 import org.contineo.core.FileBean; 12 import org.contineo.core.config.SettingConfigurator; 13 import org.contineo.core.doxter.Storer; 14 import org.contineo.core.text.lili.LanguageIdentifier; 15 import org.contineo.core.text.parser.Parser; 16 import org.contineo.core.text.parser.ParserFactory; 17 import org.contineo.core.util.IconSelector; 18 import org.contineo.documan.dao.DocumentDAO; 19 import org.contineo.documan.dao.HistoryDAO; 20 import org.contineo.searchengine.SearchDocument; 21 import org.contineo.searchengine.crawler.Indexer; 22 import org.contineo.searchengine.dao.SearchDocumentDAO; 23 24 28 public class CheckinDocUtil { 29 39 public static void checkinDocument(int docId, InputStream fileInputStream, String filename, 40 String username, Version.VERSION_TYPE versionType, String versionDesc) 41 throws Exception { 42 DocumentDAO docDao = new DocumentDAO(); 44 Document document = docDao.findByPrimaryKey(docId); 45 int menuId = document.getMenuId(); 46 MenuDAO menuDao = new MenuDAO(); 47 Menu menu = menuDao.findByPrimaryKey(menuId); 48 49 String menuPath = menu.getMenuPath() + "/" + String.valueOf(menuId); 51 SettingConfigurator settings = new SettingConfigurator(); 52 String completeDocPath = settings.getValue("docdir") + menuPath + "/"; 53 54 if (!document.getDocType().equals("zip") || !document.getDocType().equals("jar")) { 56 FileBean.renameFile(completeDocPath + menu.getMenuRef(), completeDocPath + document.getDocVersion()); 57 } 58 59 String extension = filename.substring(filename.lastIndexOf(".") + 1); 61 menu.setMenuRef(filename); 62 String icon = IconSelector.selectIcon(extension); 63 menu.setMenuIcon(icon); 64 65 Version version = createNewVersion(versionType, username, versionDesc, docId, document.getDocVersion()); 67 String newVersion = version.getVersion(); 68 69 document.setDocDate(DateBean.toCompactString()); 71 document.setDocPublisher(username); 72 document.setDocStatus(Document.DOC_CHECKED_IN); 73 document.setDocType(extension); 74 document.setCheckoutUser(""); 75 document.setMenu(menu); 76 document.addVersion(version); 77 document.setDocVersion(newVersion); 78 DocumentDAO ddao = new DocumentDAO(); 79 if (ddao.store(document) == false) 80 throw new Exception (); 81 82 Class storerClass = Class.forName(settings.getValue("storer")); 84 Storer storer = (Storer) storerClass.newInstance(); 85 storer.store(fileInputStream, menuPath, filename, newVersion); 86 87 createIndexEntry(document, menuId, filename, completeDocPath); 89 90 createHistoryEntry(docId, username, History.CHECKIN); 92 } 93 94 102 private static Version createNewVersion(Version.VERSION_TYPE versionType, 103 String username, String description, 104 int docId, String oldVersionName) { 105 Version version = new Version(); 106 String newVersionName = version.getNewVersionName(oldVersionName, versionType); 107 108 version.setDocId(docId); 109 version.setVersion(newVersionName); 110 version.setVersionComment(description); 111 version.setVersionDate(DateBean.toCompactString()); 112 version.setVersionUser(username); 113 114 return version; 115 } 116 117 118 private static void createIndexEntry(Document document, int menuId, 119 String filename, String path) throws Exception { 120 Indexer index = new Indexer(); 121 index.deleteFile(String.valueOf(menuId)); 122 index.addDirectory(new File (path + filename), document); 123 } 124 125 126 private static void createHistoryEntry(int docId, String username, String eventType) { 127 History history = new History(); 128 history.setDocId(docId); 129 history.setDate(DateBean.toCompactString()); 130 history.setUsername(username); 131 history.setEvent(eventType); 132 HistoryDAO historyDao = new HistoryDAO(); 133 historyDao.store(history); 134 } 135 136 137 140 public static Menu createDocument(File file, Menu parent, String userName) throws Exception { 141 Parser parser = ParserFactory.getParser(file); 143 StringBuffer content = null; 144 if (parser != null) 145 content = parser.getContent(); 146 if (content == null) 147 content = new StringBuffer (""); 148 149 LanguageIdentifier lili = new LanguageIdentifier(); 151 String language = lili.identify(content.toString()); 152 153 String filename = file.getName(); 155 Document doc = new Document(); 156 Keywords words = new Keywords(); 157 Version vers = new Version(); 158 Menu menu = new Menu(); 159 String ext = filename.substring(filename.lastIndexOf(".") + 1); 160 ext = ext.toLowerCase(); 161 String name = ""; 162 if (parser != null) { 163 if (parser.getTitle().length() == 0) 164 name = filename.substring(0,filename.lastIndexOf(".")); 165 else 166 name = parser.getTitle(); 167 } else { 168 name = filename; 169 } 170 menu.setMenuText(name); 171 menu.setMenuParent(parent.getMenuId()); 172 173 String icon = IconSelector.selectIcon(ext); 175 menu.setMenuIcon(icon); 176 177 menu.setMenuSort(0); 178 menu.setMenuPath(parent.getMenuPath() + "/" + parent.getMenuId()); 179 menu.setMenuType(Menu.MENUTYPE_FILE); 180 menu.setMenuHier(parent.getMenuHier() + 1); 181 menu.setMenuRef(filename); 182 menu.setMenuGroup(parent.getMenuGroup()); 183 doc.setMenu(menu); 184 doc.setDocName(name); 185 doc.setDocDate(DateBean.toCompactString()); 186 doc.setDocPublisher(userName); 187 doc.setDocStatus(Document.DOC_CHECKED_IN); 188 doc.setDocType(filename.substring(filename.lastIndexOf(".") + 1)); 189 doc.setDocVersion("1.0"); 190 doc.setSource(""); 191 if (parser != null) { 192 doc.setSourceAuthor(parser.getAuthor()); 193 String srcDate = DateBean.toCompactString(parser.getSourceDate(), language); 194 if (srcDate != null) 195 doc.setSourceDate(srcDate); 196 String keywords = parser.getKeywords(); 197 if (keywords != null && keywords.length() > 0) 198 doc.setKeywords(words.toKeywords(keywords)); 199 } 200 doc.setSourceType(""); 201 doc.setCoverage(""); 202 doc.setLanguage(language); 203 204 205 vers.setVersion("1.0"); 206 vers.setVersionComment(""); 207 vers.setVersionDate(DateBean.toCompactString()); 208 vers.setVersionUser(userName); 209 doc.addVersion(vers); 210 DocumentDAO docDao = new DocumentDAO(); 211 docDao.store(doc); 212 213 createHistoryEntry(doc.getDocId(), userName, History.STORED); 215 216 SettingConfigurator settings = new SettingConfigurator(); 218 String path = settings.getValue("docdir"); 219 if (!path.endsWith(File.pathSeparator)) 220 path += "/"; 221 path += menu.getMenuPath() + "/" + doc.getMenuId(); 222 FileBean.createDir(path); 223 FileBean.copyFile(file.getAbsolutePath(), path + "/" + filename); 224 225 226 String lang = doc.getLanguage(); 227 Indexer index = new Indexer(); 228 int luceneId = index.addFile(new File (path + "/" + filename), doc, content, language); 229 SearchDocument searchDoc = new SearchDocument(); 230 searchDoc.setLuceneId(luceneId); 231 searchDoc.setMenuId(menu.getMenuId()); 232 if (lang.equals("de")) 233 searchDoc.setIndex("german"); 234 else if (lang.equals("fr")) 235 searchDoc.setIndex("french"); 236 else 237 searchDoc.setIndex("english"); 238 SearchDocumentDAO searchDocDao = new SearchDocumentDAO(); 239 searchDocDao.store(searchDoc); 240 241 return menu; 242 } 243 244 247 public static Menu createFolder(Menu parentMenu, String menuName) { 248 Menu menu = new Menu(); 249 menu.setMenuText(menuName); 250 menu.setMenuParent(parentMenu.getMenuId()); 251 menu.setMenuSort(0); 252 menu.setMenuIcon("pages/images/folder.gif"); 253 menu.setMenuPath(parentMenu.getMenuPath() + "/" + parentMenu.getMenuId()); 254 menu.setMenuType(Menu.MENUTYPE_DIRECTORY); 255 menu.setMenuHier(parentMenu.getMenuHier() + 1); 256 menu.setMenuRef(""); 257 menu.setMenuGroup(parentMenu.getMenuGroup()); 258 MenuDAO menuDao = new MenuDAO(); 259 if (menuDao.store(menu) == false) 260 return null; 261 return menu; 262 } 263 }
| Popular Tags
|