1 package mircoders.servlet;
7 import java.lang.reflect.*;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
11 import freemarker.template.*;
12 import com.oreilly.servlet.multipart.*;
13 import com.oreilly.servlet.*;
22 import mircoders.entity.*;
23 import mircoders.storage.*;
24 import mircoders.module.*;
25 import mircoders.producer.*;
28 * ServletModuleOpenIndy -
29 * is the open-access-servlet, which is responsible for
30 * adding comments to articles &
31 * open-postings to the newswire
36 public class ServletModuleOpenIndy extends ServletModule
39 private String commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
40 private String postingFormTemplate, postingFormDoneTemplate;
41 private ModuleContent contentModule;
42 private ModuleImages imageModule;
43 private ModuleTopics themenModule;
44 private String directOp ="yes";
46 // Singelton / Kontruktor
47 private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
48 public static ServletModule getInstance() { return instance; }
50 private ServletModuleOpenIndy() {
52 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
53 commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
54 commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
55 commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate");
56 postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
57 postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
58 directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
59 mainModule = new ModuleComment(DatabaseComment.getInstance());
60 contentModule = new ModuleContent(DatabaseContent.getInstance());
61 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
62 imageModule = new ModuleImages(DatabaseImages.getInstance());
63 defaultAction="addposting";
65 catch (StorageObjectException e) {
66 theLog.printError("servletmoduleopenindy could not be initialized");
72 * Method for making a comment
75 public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
77 String aid = req.getParameter("aid"); // the article id the comment will belong to
78 if (aid!=null && !aid.equals(""))
80 SimpleHash mergeData = new SimpleHash();
82 mergeData.put("aid", aid);
83 deliver(req, res, mergeData, commentFormTemplate);
85 else throw new ServletModuleException("aid not set!");
89 * Method for inserting a comment into the Database and delivering
90 * the commentDone Page
93 public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
95 String aid = req.getParameter("to_media"); // the article id the comment will belong to
96 if (aid!=null && !aid.equals(""))
98 // ok, collecting data from form
100 HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
102 //no html in comments(for now)
103 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
104 String k=(String)i.next();
105 String v=(String)withValues.get(k);
107 withValues.put(k,StringUtil.removeHTMLTags(v));
110 withValues.put("is_published","1");
113 // EntityList com = mainModule.getByWhereClause("description='"+req.getParameter("description")+"' AND to_media='"+aid+"' AND creator='"+req.getParameter("creator")+"'", 0);
114 // if (com.getCount() > 0) {
115 // theLog.printDebugInfo("Comment dupe dropped for article "+aid);
116 // deliver(req, res, (TemplateModelRoot) null, commentFormDupeTemplate);
119 // inserting into database
120 String id = mainModule.add(withValues);
122 // producing new page
123 new ProducerContent().handle(null, null, true, false, aid);
126 int exitValue = Helper.rsync();
127 theLog.printDebugInfo("rsync:"+exitValue);
129 // redirecting to url
130 // should implement back to article
131 SimpleHash mergeData = new SimpleHash();
132 deliver(req, res, mergeData, commentFormDoneTemplate);
135 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
136 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
139 else throw new ServletModuleException("aid not set!");
144 * Method for delivering the form-Page for open posting
147 public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
149 SimpleHash mergeData = new SimpleHash();
150 String numOfMedia = req.getParameter("medianum");
151 if(numOfMedia==null||numOfMedia.equals("")){
155 int mediaNum = Integer.parseInt(numOfMedia);
156 SimpleList mediaFields = new SimpleList();
157 for(int i =0; i<mediaNum;i++){
158 Integer mNum = new Integer(i+1);
159 mediaFields.add(mNum.toString());
161 mergeData.put("medianum",numOfMedia);
162 mergeData.put("mediafields",mediaFields);
163 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
166 /** @todo popups missing */
168 mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
169 } catch (Exception e) {
170 theLog.printError("languagePopUpData failed");
172 deliver(req, res, mergeData, postingFormTemplate);
176 * Method for inserting an open posting into the Database and delivering
177 * the postingDone Page
180 public void insposting(HttpServletRequest req, HttpServletResponse res)
181 throws ServletModuleException
183 SimpleHash mergeData = new SimpleHash();
184 boolean setMedia=false;
187 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
189 HashMap withValues = mp.getParameters();
191 // call the routines that escape html
193 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
194 String k=(String)i.next();
195 String v=(String)withValues.get(k);
197 if (k.equals("content_data")){
198 //this doesn't quite work yet, so for now, all html goes
199 // withValues.put(k,StringUtil.approveHTMLTags(v));
200 withValues.put(k,StringUtil.removeHTMLTags(v));
203 withValues.put(k,StringUtil.removeHTMLTags(v));
208 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
209 withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
210 withValues.put("is_produced", "0");
211 // op-articles are immediatly published
212 withValues.put("is_published","1");
213 // if op direct article-type == newswire
214 if (directOp.equals("yes")) withValues.put("to_article_type","1");
216 // owner is openposting user
217 withValues.put("to_publisher","1");
218 if (withValues.get("creator").toString().equals(""))
219 withValues.put("creator","Anonym");
221 // inserting content into database
222 String cid = contentModule.add(withValues);
224 String[] to_topicsArr = mp.getParameterValues("to_topic");
225 if (to_topicsArr != null && to_topicsArr.length > 0) {
227 DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
228 theLog.printError("setting content_x_topic success");
229 } catch (Exception e) {
230 theLog.printError("setting content_x_topic failed");
234 // if op contains uploaddata
237 for(Iterator it = mp.requestList.iterator(); it.hasNext();){
238 MpRequest mpReq = (MpRequest)it.next();
239 String fileName = mpReq.getFilename();
240 String contentType = FileUtil.guessContentTypeFromName(fileName);
241 HashMap mediaValues = new HashMap();
243 theLog.printError("CONTENT TYPE IS: "+contentType);
245 if ((contentType==null) || (contentType=="application/octet-stream")) {
246 throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
250 String mediaTitle=(String)withValues.get("media_title"+i);
253 if (mediaTitle==null)
254 mediaTitle = (String)withValues.get("title");
256 mediaValues.put("title", mediaTitle);
257 mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
258 mediaValues.put("to_publisher", "1"); // op user
259 mediaValues.put("to_media_folder", "7"); // op media_folder
260 mediaValues.put("is_produced", "0");
261 mediaValues.put("is_published","1");
263 String wc = " mime_type='"+contentType+"'";
265 EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
267 String mediaTypeId = null;
268 String mediaStorageName = null;
269 String mediaHandlerName = null;
271 if (mediaTypesList.size() > 0) {
272 mediaTypeId = mediaTypesList.elementAt(0).getId();
273 mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
274 mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
275 mediaValues.put("to_media_type",mediaTypeId);
279 Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
280 Method m = mediaStorageClass.getMethod("getInstance", null);
281 Database mediaStorage = (Database)m.invoke(null, null);
282 Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
283 mediaEnt.setStorage(mediaStorage);
284 mediaEnt.setValues(mediaValues);
285 mediaId = mediaEnt.insert();
287 theLog.printError("done inserting!!! ");
288 Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
289 MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
290 mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
291 theLog.printError("about to run ProduceMedia"+mediaId);
293 new ProducerMedia().handle(null, null, false, false, mediaId);
295 } catch (Exception e) {
296 theLog.printError("setting uploaded_media failed: "+e.toString());
301 DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
302 theLog.printError("setting content_x_media success");
303 } catch (Exception e) {
304 theLog.printError("setting content_x_media failed");
308 theLog.printDebugInfo("Wrong file uploaded!: " + fileName);
309 throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
314 // producing openpostinglist
315 new ProducerOpenPosting().handle(null,null,false,false);
316 // producing new page
317 new ProducerContent().handle(null, null, false, false,cid);
318 //if direct op producing startpage
319 if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
322 //should be configureable
323 int exitValue = Helper.rsync();
324 theLog.printDebugInfo("rsync: "+exitValue);
327 catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
328 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
329 catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
331 deliver(req, res, mergeData, postingFormDoneTemplate);