001: package org.contineo.web.document;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005:
006: import org.contineo.core.doxter.Storer;
007: import org.contineo.core.security.Menu;
008: import org.contineo.core.security.dao.MenuDAO;
009: import org.contineo.core.text.AnalyzeText;
010: import org.contineo.core.text.parser.Parser;
011: import org.contineo.core.text.parser.ParserFactory;
012:
013: import org.contineo.util.Context;
014: import org.contineo.util.config.SettingsConfig;
015:
016: import org.contineo.web.SessionManagement;
017: import org.contineo.web.i18n.Messages;
018: import org.contineo.web.navigation.PageContentBean;
019: import org.contineo.web.upload.InputFileBean;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.InputStream;
024:
025: import java.util.Date;
026:
027: import javax.faces.application.Application;
028: import javax.faces.application.FacesMessage;
029: import javax.faces.context.FacesContext;
030:
031: /**
032: * Wizard that handled the creation of a new document.
033: *
034: * @author Marco Meschieri
035: * @version $Id: NewDocWizard.java,v 1.9 2006/09/04 15:48:35 marco Exp $
036: * @since 3.0
037: */
038: public class NewDocWizard {
039: protected static Log log = LogFactory.getLog(NewDocWizard.class);
040: private boolean showUpload = true;
041: private DocumentNavigation documentNavigation;
042:
043: public boolean isShowUpload() {
044: return showUpload;
045: }
046:
047: /**
048: * Starts the upload process
049: */
050: public String start() {
051: documentNavigation.setSelectedPanel(new PageContentBean(
052: "uploadDocument"));
053:
054: // Remove the uploaded file, if one was uploaded
055: Application application = FacesContext.getCurrentInstance()
056: .getApplication();
057: InputFileBean inputFile = ((InputFileBean) application
058: .createValueBinding("#{inputFile}").getValue(
059: FacesContext.getCurrentInstance()));
060: inputFile.deleteFile();
061:
062: showUpload = true;
063:
064: return null;
065: }
066:
067: /**
068: * Acquires the uploaded file and shows the edit form. Gets the file
069: * uploaded through the HTML form and extracts all necessary data like
070: * language, keywords, autor, etc. to fill the document form so that the
071: * user can still edit this data before finally storing the document in
072: * contineo.
073: */
074: public String next() {
075: showUpload = false;
076:
077: FacesContext facesContext = FacesContext.getCurrentInstance();
078: Application application = FacesContext.getCurrentInstance()
079: .getApplication();
080: InputFileBean inputFile = ((InputFileBean) application
081: .createValueBinding("#{inputFile}").getValue(
082: FacesContext.getCurrentInstance()));
083: DocumentEditForm docForm = ((DocumentEditForm) application
084: .createValueBinding("#{documentForm}").getValue(
085: FacesContext.getCurrentInstance()));
086: docForm.reset();
087:
088: if (SessionManagement.isValid()) {
089: try {
090: File file = inputFile.getFile();
091: String documentLanguage = inputFile.getLanguage();
092:
093: // Get menuParent that called AddDocAction
094: MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
095: MenuDAO.class);
096: Menu parent = documentNavigation.getSelectedDir()
097: .getMenu();
098:
099: // Makes menuPath
100: String menupath = new StringBuilder(parent
101: .getMenuPath()).append("/").append(
102: parent.getMenuId()).toString();
103: int menuhier = parent.getMenuHier();
104:
105: // Makes new Menu
106: Menu menu = new Menu();
107: menu.setMenuParent(parent.getMenuId());
108: menu.setMenuPath(menupath);
109: menu.setMenuHier(menuhier++);
110:
111: // and stores it
112: mdao.store(menu);
113:
114: // Gets file to upload name
115: String filename = file.getName();
116:
117: // Stores the document in the repository
118: SettingsConfig settings = (SettingsConfig) Context
119: .getInstance().getBean(SettingsConfig.class);
120: String path = new StringBuilder(settings
121: .getValue("docdir")).append(menupath).append(
122: File.separator).append(
123: String.valueOf(menu.getMenuId())).append("/")
124: .toString();
125: String mpath = new StringBuilder(menupath).append(
126: File.separator).append(
127: String.valueOf(menu.getMenuId())).toString();
128:
129: // Get file to upload inputStream
130: InputStream stream = new FileInputStream(file);
131: Storer storer = (Storer) Context.getInstance().getBean(
132: Storer.class);
133:
134: // stores it in folder
135: try {
136: storer.store(stream, mpath, filename, "1.0");
137: } finally {
138: if (stream != null) {
139: stream.close();
140: }
141: }
142:
143: File f = new File(new StringBuilder(path).append(
144: filename).toString());
145:
146: // Parses the file where it is already stored
147: Parser parser = ParserFactory.getParser(f);
148: StringBuffer content = null;
149: String name = "";
150: String author = "";
151: String keywords = "";
152:
153: // and gets some fields
154: if (parser != null) {
155: content = parser.getContent();
156: name = parser.getTitle();
157: author = parser.getAuthor();
158: keywords = parser.getKeywords();
159: }
160:
161: if (content == null) {
162: content = new StringBuffer("");
163: }
164:
165: // source field got from web.xml.
166: // This field is required for Lucene to work properly
167: String source = (String) facesContext
168: .getExternalContext().getApplicationMap().get(
169: "store");
170:
171: if (source == null) {
172: source = settings.getValue("defaultSource");
173: }
174:
175: // fills needed fields
176: if ((name != null) && (name.length() > 0)) {
177: docForm.setDocName(name);
178: } else {
179: int tmpInt = filename.lastIndexOf(".");
180:
181: if (tmpInt != -1) {
182: docForm.setDocName(filename
183: .substring(0, tmpInt));
184: } else {
185: docForm.setDocName(filename);
186: }
187: }
188:
189: docForm.setMenuParent(parent.getMenuId());
190: docForm.setSource(source);
191:
192: if (author != null) {
193: docForm.setSourceAuthor(author);
194: }
195:
196: docForm.setSourceDate(new Date());
197: docForm.setLanguage(documentLanguage);
198:
199: if ((keywords != null) && !keywords.trim().equals("")) {
200: docForm.setKeywords(keywords);
201: } else {
202: AnalyzeText analyzer = new AnalyzeText();
203: docForm.setKeywords(analyzer.getTerms(5, content
204: .toString(), documentLanguage));
205: }
206:
207: docForm.setFilename(filename);
208: docForm.setMenu(menu);
209:
210: // update the file size
211: mdao.store(menu);
212: } catch (Exception e) {
213: String message = Messages
214: .getMessage("errors.action.savedoc");
215: log.error(message, e);
216: Messages.addMessage(FacesMessage.SEVERITY_ERROR,
217: message, message);
218: showUpload = true;
219: }
220:
221: return null;
222: } else {
223: return "login";
224: }
225: }
226:
227: public String abort() {
228: documentNavigation.setSelectedPanel(new PageContentBean(
229: "documents"));
230:
231: // Remove the uploaded file, if one was uploaded
232: Application application = FacesContext.getCurrentInstance()
233: .getApplication();
234: InputFileBean inputFile = ((InputFileBean) application
235: .createValueBinding("#{inputFile}").getValue(
236: FacesContext.getCurrentInstance()));
237: inputFile.deleteFile();
238:
239: return null;
240: }
241:
242: public String save() {
243: Application application = FacesContext.getCurrentInstance()
244: .getApplication();
245: DocumentEditForm documentForm = ((DocumentEditForm) application
246: .createValueBinding("#{documentForm}").getValue(
247: FacesContext.getCurrentInstance()));
248:
249: documentForm.save();
250: documentNavigation.selectDirectory(documentNavigation
251: .getSelectedDir());
252:
253: return abort();
254: }
255:
256: public void setDocumentNavigation(
257: DocumentNavigation documentNavigation) {
258: this.documentNavigation = documentNavigation;
259: }
260: }
|