KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > documan > CheckinDocUtil


1 /* License: GNU General Public License (GPL) version 2 from June 1991; but not any newer version!
2  */

3 package org.contineo.documan;
4
5 import java.io.File JavaDoc;
6 import java.io.InputStream JavaDoc;
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 /**
25  * utility class to checkin/create a document
26  * @author Sebastian Stein
27  */

28 public class CheckinDocUtil {
29     /**
30      * checks in the given document
31      * @param docId the document to be checked in
32      * @param fileInputStream input stream pointing to the new document version
33      * @param filename new filename (can also be the old one)
34      * @param username user uploading the new document version
35      * @param versionType specifies if this is a new release, a subversion or the old version
36      * @param versionDesc a change description
37      * @throws Exception if an error occurs, this exception is thrown
38      */

39     public static void checkinDocument(int docId, InputStream JavaDoc fileInputStream, String JavaDoc filename,
40                                     String JavaDoc username, Version.VERSION_TYPE versionType, String JavaDoc versionDesc)
41             throws Exception JavaDoc {
42         // identify the document and menu
43
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         // create some strings containing paths
50
String JavaDoc menuPath = menu.getMenuPath() + "/" + String.valueOf(menuId);
51         SettingConfigurator settings = new SettingConfigurator();
52         String JavaDoc completeDocPath = settings.getValue("docdir") + menuPath + "/";
53         
54         // rename the old current version file to the version name: "quelle.txt" -> "2.0"
55
if (!document.getDocType().equals("zip") || !document.getDocType().equals("jar")) {
56             FileBean.renameFile(completeDocPath + menu.getMenuRef(), completeDocPath + document.getDocVersion());
57         }
58         
59         // extract file extension of the new file and select a file icon based on the extension
60
String JavaDoc extension = filename.substring(filename.lastIndexOf(".") + 1);
61         menu.setMenuRef(filename);
62         String JavaDoc icon = IconSelector.selectIcon(extension);
63         menu.setMenuIcon(icon);
64         
65         // create new version
66
Version version = createNewVersion(versionType, username, versionDesc, docId, document.getDocVersion());
67         String JavaDoc newVersion = version.getVersion();
68         
69         // set other properties of the document
70
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 JavaDoc();
81
82         // store the document in the repository (on the file system)
83
Class JavaDoc storerClass = Class.forName(settings.getValue("storer"));
84         Storer storer = (Storer) storerClass.newInstance();
85         storer.store(fileInputStream, menuPath, filename, newVersion);
86
87         // create search index entry
88
createIndexEntry(document, menuId, filename, completeDocPath);
89
90         // create history entry for this checkin event
91
createHistoryEntry(docId, username, History.CHECKIN);
92     }
93
94     /**
95      * creates a new version object and fills in the provided attributes
96      * @param versionType either a new release, a new subversion or just the old version
97      * @param username user creating the new version
98      * @param description change description
99      * @param docId version should belong to this document
100      * @param oldVersionName the previous version name
101      */

102     private static Version createNewVersion(Version.VERSION_TYPE versionType,
103                                             String JavaDoc username, String JavaDoc description,
104                                             int docId, String JavaDoc oldVersionName) {
105         Version version = new Version();
106         String JavaDoc 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     /** creates a new search index entry for the given document */
118     private static void createIndexEntry(Document document, int menuId,
119                                             String JavaDoc filename, String JavaDoc path) throws Exception JavaDoc {
120         Indexer index = new Indexer();
121         index.deleteFile(String.valueOf(menuId));
122         index.addDirectory(new File JavaDoc(path + filename), document);
123     }
124
125     /** creates history entry saying username has checked in document (id) */
126     private static void createHistoryEntry(int docId, String JavaDoc username, String JavaDoc 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     /**
138      * creates a new document in the parent menu
139      */

140     public static Menu createDocument(File JavaDoc file, Menu parent, String JavaDoc userName) throws Exception JavaDoc {
141         // extract content
142
Parser parser = ParserFactory.getParser(file);
143         StringBuffer JavaDoc content = null;
144         if (parser != null)
145             content = parser.getContent();
146         if (content == null)
147             content = new StringBuffer JavaDoc("");
148         
149         // identify language
150
LanguageIdentifier lili = new LanguageIdentifier();
151         String JavaDoc language = lili.identify(content.toString());
152         
153         // store in database
154
String JavaDoc 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 JavaDoc ext = filename.substring(filename.lastIndexOf(".") + 1);
160         ext = ext.toLowerCase();
161         String JavaDoc 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         // select a file icon based on the extension
174
String JavaDoc 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 JavaDoc srcDate = DateBean.toCompactString(parser.getSourceDate(), language);
194             if (srcDate != null)
195                 doc.setSourceDate(srcDate);
196             String JavaDoc 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         /* insert initial version 1.0 */
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         // create history entry
214
createHistoryEntry(doc.getDocId(), userName, History.STORED);
215         
216         // store document in the repository
217
SettingConfigurator settings = new SettingConfigurator();
218         String JavaDoc 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         /* create search index entry */
226         String JavaDoc lang = doc.getLanguage();
227         Indexer index = new Indexer();
228         int luceneId = index.addFile(new File JavaDoc(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     /**
245      * creates a new folder in the parent menu
246      */

247     public static Menu createFolder(Menu parentMenu, String JavaDoc 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