1 package org.contineo.actions.documan.document; 2 3 import java.io.File ; 4 import java.io.InputStream ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 import javax.servlet.http.HttpSession ; 11 12 import org.apache.log4j.Level; 13 import org.apache.log4j.Logger; 14 import org.apache.struts.action.Action; 15 import org.apache.struts.action.ActionError; 16 import org.apache.struts.action.ActionErrors; 17 import org.apache.struts.action.ActionForm; 18 import org.apache.struts.action.ActionForward; 19 import org.apache.struts.action.ActionMapping; 20 import org.apache.struts.action.ActionMessages; 21 import org.apache.struts.upload.FormFile; 22 import org.contineo.admin.Group; 23 import org.contineo.admin.Menu; 24 import org.contineo.admin.dao.GroupDAO; 25 import org.contineo.admin.dao.MenuDAO; 26 import org.contineo.core.DateBean; 27 import org.contineo.core.LoggingManager; 28 import org.contineo.core.SessionManagement; 29 import org.contineo.core.config.SettingConfigurator; 30 import org.contineo.core.doxter.Storer; 31 import org.contineo.core.text.AnalyzeText; 32 import org.contineo.core.text.lili.LanguageIdentifier; 33 import org.contineo.core.text.parser.Parser; 34 import org.contineo.core.text.parser.ParserFactory; 35 import org.contineo.forms.DocForm; 36 import org.contineo.forms.UploadForm; 37 38 45 public class SaveUploadAction extends Action { 46 47 private Logger logger; 48 49 public SaveUploadAction() { 50 logger = LoggingManager.getLogger(this.getClass()); 51 } 52 53 public ActionForward execute(ActionMapping mapping, 54 ActionForm form, HttpServletRequest request, 55 HttpServletResponse response) { 56 ActionForward actionForward = new ActionForward(); 57 ActionErrors errors = new ActionErrors(); 58 ActionMessages messages = new ActionMessages(); 59 HttpSession session = request.getSession(); 60 if (SessionManagement.isValid(session)) { 61 try { 62 UploadForm uForm = (UploadForm)form; 63 MenuDAO mdao = new MenuDAO(); 64 Menu parent = mdao.findByPrimaryKey(Integer.parseInt(uForm.getMenuParent())); 65 if (uForm.getFile() == null || uForm.getFile().getFileName().equals("")) { 66 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.required")); 67 actionForward.setPath("/AddDoc.do?parentid=" + String.valueOf(parent.getMenuId()) + 68 "&parentname=" + parent.getMenuText() + "&parenttype=" + String.valueOf(parent.getMenuType())); 69 } else { 70 String menupath = parent.getMenuPath() + "/" + parent.getMenuId(); 71 int menuhier = parent.getMenuHier(); 72 Menu menu = new Menu(); 73 menu.setMenuParent(Integer.parseInt(uForm.getMenuParent())); 74 menu.setMenuPath(menupath); 75 menu.setMenuHier(menuhier++); 76 mdao.store(menu); 77 FormFile file = uForm.getFile(); 78 String filename = file.getFileName(); 79 80 81 SettingConfigurator settings = new SettingConfigurator(); 82 String path = settings.getValue("docdir"); 83 path += menupath + "/" + String.valueOf(menu.getMenuId()) + "/"; 84 String mpath = menupath + "/" + String.valueOf(menu.getMenuId()); 85 InputStream stream = file.getInputStream(); 86 Class clazz = Class.forName(settings.getValue("storer")); 87 Storer storer = (Storer)clazz.newInstance(); 88 storer.store(stream, mpath, filename, "1.0"); 89 File f = new File (path + filename); 90 Parser parser = ParserFactory.getParser(f); 91 StringBuffer content = null; 92 String name = ""; 93 String author = ""; 94 String sourceDate = ""; 95 String keywords = ""; 96 if (parser != null) { 97 content = parser.getContent(); 98 name = parser.getTitle(); 99 author = parser.getAuthor(); 100 sourceDate = parser.getSourceDate(); 101 keywords = parser.getKeywords(); 102 } 103 if (content == null) { 104 content = new StringBuffer (""); 105 } 106 session.setAttribute("content", content); 107 request.setAttribute("parentname", parent.getMenuText()); 108 request.setAttribute("parenttype", String.valueOf(parent.getMenuType())); 109 DocForm docform = new DocForm(); 110 LanguageIdentifier lili = new LanguageIdentifier(); 111 String language = lili.identify(content.toString()); 112 docform.setMenuParent(uForm.getMenuParent()); 113 if (name != null && name.length() > 0) 114 docform.setDocName(name); 115 else { 116 int tmpInt = filename.lastIndexOf("."); 117 if (tmpInt != -1) 118 docform.setDocName(filename.substring(0, tmpInt)); 119 else 120 docform.setDocName(filename); 121 } 122 if (author != null) 123 docform.setSourceAuthor(author); 124 if (sourceDate != null) { 125 String dateStr = DateBean.toCompactString(sourceDate, language); 126 if (dateStr != null) 127 docform.setSourceDate(dateStr); 128 } 129 if (keywords != null && !keywords.trim().equals("")) 130 docform.setKeywords(keywords); 131 else { 132 AnalyzeText analyzer = new AnalyzeText(); 133 docform.setKeywords(analyzer.getTerms(5, content.toString(), language)); 134 } 135 docform.setMenuid(String.valueOf(menu.getMenuId())); 136 docform.setMenuParent(uForm.getMenuParent()); 137 docform.setLanguage(language); 138 docform.setFilename(filename); 139 request.setAttribute("docform", docform); 140 GroupDAO dao = new GroupDAO(); 141 Collection coll = dao.findAll(); 142 143 for (Iterator iter = coll.iterator(); iter.hasNext();) { 145 Group currGroup = (Group) iter.next(); 146 if (currGroup.getGroupName().equals("admin")) { 147 iter.remove(); 148 } 149 } 150 151 request.setAttribute("groups", coll); 152 actionForward = mapping.findForward("adddoc"); 153 } 154 } catch (Exception e) { 155 if (logger.isEnabledFor(Level.ERROR)) 156 logger.error(e.getMessage()); 157 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.action.savedoc")); 158 actionForward = mapping.findForward("error"); 159 } 160 saveErrors(request, errors); 161 saveMessages(request, messages); 162 } else 163 actionForward = mapping.findForward("invalid"); 164 return actionForward; 165 } 166 } 167 | Popular Tags |