KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > documan > document > SaveDocAction


1 package org.contineo.actions.documan.document;
2
3 import java.io.File JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.http.HttpSession JavaDoc;
10
11 import org.apache.log4j.Level;
12 import org.apache.log4j.Logger;
13 import org.apache.struts.action.Action;
14 import org.apache.struts.action.ActionError;
15 import org.apache.struts.action.ActionErrors;
16 import org.apache.struts.action.ActionForm;
17 import org.apache.struts.action.ActionForward;
18 import org.apache.struts.action.ActionMapping;
19 import org.apache.struts.action.ActionMessage;
20 import org.apache.struts.action.ActionMessages;
21 import org.contineo.admin.Group;
22 import org.contineo.admin.Menu;
23 import org.contineo.admin.dao.GroupDAO;
24 import org.contineo.admin.dao.MenuDAO;
25 import org.contineo.core.DateBean;
26 import org.contineo.core.LoggingManager;
27 import org.contineo.core.SessionManagement;
28 import org.contineo.core.config.SettingConfigurator;
29 import org.contineo.core.util.IconSelector;
30 import org.contineo.documan.Document;
31 import org.contineo.documan.History;
32 import org.contineo.documan.Keywords;
33 import org.contineo.documan.Version;
34 import org.contineo.documan.dao.DocumentDAO;
35 import org.contineo.documan.dao.HistoryDAO;
36 import org.contineo.forms.DocForm;
37 import org.contineo.searchengine.SearchDocument;
38 import org.contineo.searchengine.crawler.Indexer;
39 import org.contineo.searchengine.dao.SearchDocumentDAO;
40
41 /**
42  * Either fills the document form with information or saves the information
43  * provided in the document form. That also includes updating the search index for example.
44  * Created on 28. August 2003, 12:12
45  * @author Michael Scholz, Sebastian Stein
46  */

47 public class SaveDocAction extends Action {
48
49     /**
50      * @uml.property name="logger"
51      * @uml.associationEnd
52      */

53     private Logger logger;
54
55     
56     /** Creates a new instance of SaveDocAction */
57     public SaveDocAction() {
58         logger = LoggingManager.getLogger(this.getClass());
59     }
60  
61     public ActionForward execute(ActionMapping mapping,
62                     ActionForm form, HttpServletRequest JavaDoc request,
63                     HttpServletResponse JavaDoc response) {
64         ActionForward actionForward = new ActionForward();
65         ActionErrors errors = new ActionErrors();
66         ActionMessages messages = new ActionMessages();
67         HttpSession JavaDoc session = request.getSession();
68         if (SessionManagement.isValid(session)) {
69             DocForm docform = (DocForm)form;
70             MenuDAO mdao = new MenuDAO();
71             DocumentDAO ddao = new DocumentDAO();
72             Menu parent = new Menu();
73             try {
74                 parent = mdao.findByPrimaryKey(Integer.parseInt(docform.getMenuParent()));
75                 Document doc = new Document();
76                 Keywords words = new Keywords();
77                 Version vers = new Version();
78                 if (docform.getMenuGroup() == null || docform.getMenuGroup().length == 0) {
79                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.required"));
80                     request.setAttribute("parentname", parent.getMenuText());
81                     request.setAttribute("parenttype", String.valueOf(parent.getMenuType()));
82                     request.setAttribute("docform", docform);
83                     GroupDAO dao = new GroupDAO();
84                     Collection JavaDoc coll = dao.findAll();
85
86                     // do not include admin group in list
87
for (Iterator JavaDoc iter = coll.iterator(); iter.hasNext();) {
88                         Group currGroup = (Group) iter.next();
89                         if (currGroup.getGroupName().equals("admin")) {
90                             iter.remove();
91                         }
92                     }
93                     
94                     request.setAttribute("groups", coll);
95                     actionForward = mapping.findForward("adddoc");
96                 } else {
97                     Menu menu = mdao.findByPrimaryKey(Integer.parseInt(docform.getMenuid()));
98                     
99                     // extract file extension of the new file and select a file icon based on the extension
100
String JavaDoc filename = docform.getFilename();
101                     String JavaDoc ext = filename.substring(filename.lastIndexOf(".") + 1);
102                     String JavaDoc name = docform.getDocName();
103                     if (name != null && !name.equals(""))
104                         menu.setMenuText(name);
105                     else
106                         menu.setMenuText(filename.substring(0,filename.lastIndexOf(".")));
107                     
108                     String JavaDoc icon = IconSelector.selectIcon(ext);
109                     menu.setMenuIcon(icon);
110                     
111                     menu.setMenuSort(Integer.parseInt(docform.getMenuSort()));
112                     menu.setMenuType(Menu.MENUTYPE_FILE);
113                     menu.setMenuRef(filename);
114                     
115                     // by default add read and write privileges to admin group
116
String JavaDoc[] docGroup = docform.getMenuGroup();
117                     boolean hasAdmin = false;
118                     for (int i = 0; i < docGroup.length; i++) {
119                         if (docGroup[i].equals("admin")) {
120                             hasAdmin = true;
121                             break;
122                         }
123                     }
124                     if (!hasAdmin) {
125                         String JavaDoc[] docGroupAdmin = new String JavaDoc[docGroup.length + 1];
126                         docGroupAdmin[0] = "admin";
127                         for (int i = 0; i < docGroup.length; i++) {
128                             docGroupAdmin[i + 1] = docGroup[i];
129                         }
130                         menu.setMenuGroup(docGroupAdmin);
131                     } else {
132                         menu.setMenuGroup(docGroup);
133                     }
134                     
135                     doc.setMenu(menu);
136                     if (name != null && !name.equals(""))
137                         doc.setDocName(name);
138                     else
139                         doc.setDocName(filename.substring(0,filename.lastIndexOf(".")));
140                     doc.setDocDate(DateBean.toCompactString());
141                     doc.setDocPublisher((String JavaDoc)session.getAttribute("authuser"));
142                     doc.setDocStatus(Document.DOC_CHECKED_IN);
143                     doc.setDocType(filename.substring(filename.lastIndexOf(".") + 1));
144                     doc.setDocVersion("1.0");
145                     doc.setSource(docform.getSource());
146                     doc.setSourceAuthor(docform.getSourceAuthor());
147                     
148                     // get current language
149
String JavaDoc language = (String JavaDoc) session.getAttribute("language");
150                     if (language == null || language.equalsIgnoreCase(""))
151                     {
152                         language = request.getParameter("language");
153                         if (language == null || language.equalsIgnoreCase(""))
154                             language = "en";
155                     }
156                     String JavaDoc dateStr = DateBean.toCompactString(docform.getSourceDate(), language);
157                     if (dateStr != null)
158                         doc.setSourceDate(dateStr);
159                     doc.setSourceType(docform.getSourceType());
160                     doc.setCoverage(docform.getCoverage());
161                     doc.setLanguage(docform.getLanguage());
162                     /* insert initial version 1.0 */
163                     vers.setVersion("1.0");
164                     vers.setVersionComment(docform.getVersionDesc());
165                     vers.setVersionDate(DateBean.toCompactString());
166                     String JavaDoc username = (String JavaDoc)session.getAttribute("authuser");
167                     vers.setVersionUser(username);
168                     doc.addVersion(vers);
169                     /* create keywords for the document */
170                     Collection JavaDoc keywords = words.toKeywords(docform.getKeywords());
171                     doc.setKeywords(keywords);
172                     boolean stored = ddao.store(doc);
173
174                     /* create history entry */
175                     History history = new History();
176                     history.setDocId(doc.getDocId());
177                     history.setDate(DateBean.toCompactString());
178                     history.setUsername(username);
179                     history.setEvent(History.STORED);
180                     HistoryDAO historyDao = new HistoryDAO();
181                     historyDao.store(history);
182
183                     String JavaDoc menupath = menu.getMenuPath();
184                     SettingConfigurator settings = new SettingConfigurator();
185                     String JavaDoc path = settings.getValue("docdir");
186                     path += menupath + "/" + String.valueOf(menu.getMenuId()) + "/";
187                     /* create search index entry */
188                     String JavaDoc lang = doc.getLanguage();
189                     Indexer index = new Indexer();
190                     int luceneId = index.addFile(new File JavaDoc(path + "/" + menu.getMenuRef()), doc, (StringBuffer JavaDoc)session.getAttribute("content"), docform.getLanguage());
191                     SearchDocument searchDoc = new SearchDocument();
192                     searchDoc.setLuceneId(luceneId);
193                     searchDoc.setMenuId(menu.getMenuId());
194                     if (lang.equals("de"))
195                         searchDoc.setIndex("german");
196                     else if (lang.equals("fr"))
197                         searchDoc.setIndex("french");
198                     else
199                         searchDoc.setIndex("english");
200                     SearchDocumentDAO searchDocDao = new SearchDocumentDAO();
201                     searchDocDao.store(searchDoc);
202                     /* create messages */
203                     if (!stored)
204                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.action.savedoc"));
205                     else
206                         messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("msg.action.savedoc"));
207                     actionForward.setPath("/AddDoc.do?parentid=" + String.valueOf(parent.getMenuId()) +
208                             "&parentname=" + parent.getMenuText() + "&parenttype=" + String.valueOf(parent.getMenuType()));
209                 }
210             } catch (Exception JavaDoc e) {
211                 if (logger.isEnabledFor(Level.ERROR))
212                     logger.error(e.getMessage());
213                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.action.savedoc"));
214                 actionForward.setPath("/AddDoc.do?parentid=" + String.valueOf(parent.getMenuId()) +
215                         "&parentname=" + parent.getMenuText() + "&parenttype=" + String.valueOf(parent.getMenuType()));
216             }
217             saveErrors(request, errors);
218             saveMessages(request, messages);
219         } else
220             actionForward = mapping.findForward("invalid");
221         return actionForward;
222     }
223 }
224
Popular Tags