001: package org.contineo.core.document;
002:
003: import java.io.File;
004: import java.io.InputStream;
005: import java.util.Locale;
006:
007: import org.apache.commons.lang.StringUtils;
008: import org.contineo.core.FileBean;
009: import org.contineo.core.document.dao.DocumentDAO;
010: import org.contineo.core.document.dao.HistoryDAO;
011: import org.contineo.core.doxter.Storer;
012: import org.contineo.core.i18n.DateBean;
013: import org.contineo.core.searchengine.SearchDocument;
014: import org.contineo.core.searchengine.crawler.Indexer;
015: import org.contineo.core.searchengine.dao.SearchDocumentDAO;
016: import org.contineo.core.security.Menu;
017: import org.contineo.core.security.MenuGroup;
018: import org.contineo.core.security.dao.MenuDAO;
019: import org.contineo.core.text.parser.Parser;
020: import org.contineo.core.text.parser.ParserFactory;
021: import org.contineo.core.util.IconSelector;
022: import org.contineo.util.Context;
023: import org.contineo.util.config.SettingsConfig;
024:
025: /**
026: * utility class to checkin/create a document
027: *
028: * @author Sebastian Stein
029: */
030: public class CheckinDocUtil {
031: /**
032: * checks in the given document
033: *
034: * @param docId the document to be checked in
035: * @param fileInputStream input stream pointing to the new document version
036: * @param filename new filename (can also be the old one)
037: * @param username user uploading the new document version
038: * @param versionType specifies if this is a new release, a subversion or
039: * the old version
040: * @param versionDesc a change description
041: * @throws Exception if an error occurs, this exception is thrown
042: */
043: public static void checkinDocument(int docId,
044: InputStream fileInputStream, String filename,
045: String username, Version.VERSION_TYPE versionType,
046: String versionDesc) throws Exception {
047: // identify the document and menu
048: DocumentDAO docDao = (DocumentDAO) Context.getInstance()
049: .getBean(DocumentDAO.class);
050: Document document = docDao.findByPrimaryKey(docId);
051: int menuId = document.getMenuId();
052: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
053: MenuDAO.class);
054: Menu menu = menuDao.findByPrimaryKey(menuId);
055:
056: // create some strings containing paths
057: String menuPath = menu.getMenuPath() + "/"
058: + String.valueOf(menuId);
059: SettingsConfig settings = (SettingsConfig) Context
060: .getInstance().getBean(SettingsConfig.class);
061: String completeDocPath = settings.getValue("docdir") + menuPath
062: + "/";
063:
064: // rename the old current version file to the version name: "quelle.txt"
065: // -> "2.0"
066: if (!document.getDocType().equals("zip")
067: || !document.getDocType().equals("jar")) {
068: FileBean.renameFile(completeDocPath + menu.getMenuRef(),
069: completeDocPath + document.getDocVersion());
070: }
071:
072: // extract file extension of the new file and select a file icon based
073: // on the extension
074: String extension = filename
075: .substring(filename.lastIndexOf(".") + 1);
076: menu.setMenuRef(filename);
077: String icon = IconSelector.selectIcon(extension);
078: menu.setMenuIcon(icon);
079:
080: // create new version
081: Version version = createNewVersion(versionType, username,
082: versionDesc, document.getDocVersion());
083: String newVersion = version.getVersion();
084:
085: // set other properties of the document
086: document.setDocDate(DateBean.toCompactString());
087: document.setDocPublisher(username);
088: document.setDocStatus(Document.DOC_CHECKED_IN);
089: document.setDocType(extension);
090: document.setCheckoutUser("");
091: document.setMenu(menu);
092: document.addVersion(version);
093: document.setDocVersion(newVersion);
094: DocumentDAO ddao = (DocumentDAO) Context.getInstance().getBean(
095: DocumentDAO.class);
096: if (ddao.store(document) == false)
097: throw new Exception();
098:
099: // store the document in the repository (on the file system)
100: Storer storer = (Storer) Context.getInstance().getBean(
101: Storer.class);
102: storer.store(fileInputStream, menuPath, filename, newVersion);
103:
104: // create search index entry
105: createIndexEntry(document, menuId, filename, completeDocPath);
106:
107: // create history entry for this checkin event
108: createHistoryEntry(docId, username, History.CHECKIN);
109: }
110:
111: /**
112: * creates a new version object and fills in the provided attributes
113: *
114: * @param versionType either a new release, a new subversion or just the old
115: * version
116: * @param username user creating the new version
117: * @param description change description
118: * @param docId version should belong to this document
119: * @param oldVersionName the previous version name
120: */
121: private static Version createNewVersion(
122: Version.VERSION_TYPE versionType, String username,
123: String description, String oldVersionName) {
124: Version version = new Version();
125: String newVersionName = version.getNewVersionName(
126: oldVersionName, versionType);
127:
128: version.setVersion(newVersionName);
129: version.setVersionComment(description);
130: version.setVersionDate(DateBean.toCompactString());
131: version.setVersionUser(username);
132:
133: return version;
134: }
135:
136: /** creates a new search index entry for the given document */
137: private static void createIndexEntry(Document document, int menuId,
138: String filename, String path) throws Exception {
139: Indexer index = (Indexer) Context.getInstance().getBean(
140: Indexer.class);
141: index
142: .deleteFile(String.valueOf(menuId), document
143: .getLanguage());
144: index.addDirectory(new File(path + filename), document);
145: }
146:
147: /** creates history entry saying username has checked in document (id) */
148: private static void createHistoryEntry(int docId, String username,
149: String eventType) {
150: History history = new History();
151: history.setDocId(docId);
152: history.setDate(DateBean.toCompactString());
153: history.setUsername(username);
154: history.setEvent(eventType);
155: HistoryDAO historyDao = (HistoryDAO) Context.getInstance()
156: .getBean(HistoryDAO.class);
157: historyDao.store(history);
158: }
159:
160: /**
161: * creates a new document in the parent menu
162: */
163: public static Menu createDocument(File file, Menu parent,
164: String userName, String language) throws Exception {
165: // extract content
166: Parser parser = ParserFactory.getParser(file);
167: StringBuffer content = null;
168: if (parser != null)
169: content = parser.getContent();
170: if (content == null)
171: content = new StringBuffer("");
172:
173: // store in database
174: String filename = file.getName();
175: Document doc = new Document();
176: Version vers = new Version();
177: Menu menu = new Menu();
178: String ext = filename.substring(filename.lastIndexOf(".") + 1);
179: ext = ext.toLowerCase();
180: String name = "";
181: if (parser != null) {
182: if (parser.getTitle().length() == 0)
183: name = filename.substring(0, filename.lastIndexOf("."));
184: else
185: name = parser.getTitle();
186: } else {
187: name = filename;
188: }
189: menu.setMenuText(name);
190: menu.setMenuParent(parent.getMenuId());
191:
192: // select a file icon based on the extension
193: String icon = IconSelector.selectIcon(ext);
194: menu.setMenuIcon(icon);
195:
196: menu.setMenuSort(0);
197: menu.setMenuPath(parent.getMenuPath() + "/"
198: + parent.getMenuId());
199: menu.setMenuType(Menu.MENUTYPE_FILE);
200: menu.setMenuHier(parent.getMenuHier() + 1);
201: menu.setMenuRef(filename);
202: for (MenuGroup mg : parent.getMenuGroups()) {
203: menu.getMenuGroups().add(mg);
204: }
205:
206: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
207: MenuDAO.class);
208: menuDao.store(menu);
209:
210: doc.setMenu(menu);
211: doc.setDocName(name);
212: doc.setDocDate(DateBean.toCompactString());
213: doc.setDocPublisher(userName);
214: doc.setDocStatus(Document.DOC_CHECKED_IN);
215: doc.setDocType(filename
216: .substring(filename.lastIndexOf(".") + 1));
217: doc.setDocVersion("1.0");
218: doc.setSource("");
219: if (parser != null) {
220: doc.setSourceAuthor(parser.getAuthor());
221: String srcDate = DateBean.toCompactString(parser
222: .getSourceDate(), language);
223: if (srcDate != null)
224: doc.setSourceDate(srcDate);
225: String keywords = parser.getKeywords();
226: if (keywords != null && keywords.length() > 0) {
227: DocumentDAO docDao = (DocumentDAO) Context
228: .getInstance().getBean(DocumentDAO.class);
229: doc.setKeywords(docDao.toKeywords(keywords));
230: }
231: }
232: doc.setSourceType("");
233: doc.setCoverage("");
234: doc.setLanguage(language);
235:
236: /* insert initial version 1.0 */
237: vers.setVersion("1.0");
238: vers.setVersionComment("");
239: vers.setVersionDate(DateBean.toCompactString());
240: vers.setVersionUser(userName);
241: doc.addVersion(vers);
242: DocumentDAO docDao = (DocumentDAO) Context.getInstance()
243: .getBean(DocumentDAO.class);
244: docDao.store(doc);
245:
246: // create history entry
247: createHistoryEntry(doc.getDocId(), userName, History.STORED);
248:
249: // store document in the repository
250: SettingsConfig settings = (SettingsConfig) Context
251: .getInstance().getBean(SettingsConfig.class);
252: String path = settings.getValue("docdir");
253: if (!path.endsWith(File.pathSeparator))
254: path += "/";
255: path += menu.getMenuPath() + "/" + doc.getMenuId();
256: FileBean.createDir(path);
257: FileBean
258: .copyFile(file.getAbsolutePath(), path + "/" + filename);
259:
260: /* create search index entry */
261: String lang = doc.getLanguage();
262: Indexer index = (Indexer) Context.getInstance().getBean(
263: Indexer.class);
264: int luceneId = index.addFile(new File(path + "/" + filename),
265: doc, content, language);
266: SearchDocument searchDoc = new SearchDocument();
267: searchDoc.setLuceneId(luceneId);
268: searchDoc.setMenuId(menu.getMenuId());
269:
270: String luceneIndex = new Locale(lang).getDisplayLanguage(
271: Locale.ENGLISH).toLowerCase();
272: if (StringUtils.isEmpty(luceneIndex))
273: luceneIndex = "english";
274:
275: searchDoc.setIndex(luceneIndex);
276:
277: SearchDocumentDAO searchDocDao = (SearchDocumentDAO) Context
278: .getInstance().getBean(SearchDocumentDAO.class);
279: searchDocDao.store(searchDoc);
280:
281: //Update file size
282: menuDao.store(menu);
283:
284: return menu;
285: }
286:
287: /**
288: * creates a new folder in the parent menu
289: */
290: public static Menu createFolder(Menu parentMenu, String menuName) {
291: Menu menu = new Menu();
292: menu.setMenuText(menuName);
293: menu.setMenuParent(parentMenu.getMenuId());
294: menu.setMenuSort(0);
295: menu.setMenuIcon("folder.gif");
296: menu.setMenuPath(parentMenu.getMenuPath() + "/"
297: + parentMenu.getMenuId());
298: menu.setMenuType(Menu.MENUTYPE_DIRECTORY);
299: menu.setMenuHier(parentMenu.getMenuHier() + 1);
300: menu.setMenuRef("");
301: for (MenuGroup mg : parentMenu.getMenuGroups()) {
302: menu.getMenuGroups().add(mg);
303: }
304:
305: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
306: MenuDAO.class);
307: if (menuDao.store(menu) == false)
308: return null;
309: return menu;
310: }
311: }
|