1 package mircoders.servlet;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
9 import freemarker.template.*;
10 import com.oreilly.servlet.multipart.*;
11 import com.oreilly.servlet.*;
19 import mircoders.entity.*;
20 import mircoders.storage.*;
21 import mircoders.module.*;
22 import mircoders.producer.*;
25 * ServletModuleOpenIndy -
26 * is the open-access-servlet, which is responsible for
27 * adding comments to articles &
28 * open-postings to the newswire
33 public class ServletModuleOpenIndy extends ServletModule
36 private String commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
37 private String postingFormTemplate, postingFormDoneTemplate;
38 private ModuleContent contentModule;
39 private ModuleImages imageModule;
40 private ModuleTopics themenModule;
41 private String directOp ="yes";
43 // Singelton / Kontruktor
44 private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
45 public static ServletModule getInstance() { return instance; }
47 private ServletModuleOpenIndy() {
49 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
50 commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
51 commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
52 commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate");
53 postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
54 postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
55 directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
56 mainModule = new ModuleComment(DatabaseComment.getInstance());
57 contentModule = new ModuleContent(DatabaseContent.getInstance());
58 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
59 imageModule = new ModuleImages(DatabaseImages.getInstance());
60 defaultAction="addposting";
62 catch (StorageObjectException e) {
63 theLog.printError("servletmoduleopenindy could not be initialized");
69 * Method for making a comment
72 public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
74 String aid = req.getParameter("aid"); // the article id the comment will belong to
75 if (aid!=null && !aid.equals(""))
77 SimpleHash mergeData = new SimpleHash();
79 mergeData.put("aid", aid);
80 deliver(req, res, mergeData, commentFormTemplate);
82 else throw new ServletModuleException("aid not set!");
86 * Method for inserting a comment into the Database and delivering
87 * the commentDone Page
90 public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
92 String aid = req.getParameter("to_media"); // the article id the comment will belong to
93 if (aid!=null && !aid.equals(""))
95 // ok, collecting data from form
97 HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
98 withValues.put("is_published","1");
101 // EntityList com = mainModule.getByWhereClause("description='"+req.getParameter("description")+"' AND to_media='"+aid+"' AND creator='"+req.getParameter("creator")+"'", 0);
102 // if (com.getCount() > 0) {
103 // theLog.printDebugInfo("Comment dupe dropped for article "+aid);
104 // deliver(req, res, (TemplateModelRoot) null, commentFormDupeTemplate);
107 // inserting into database
108 String id = mainModule.add(withValues);
110 // producing new page
111 new ProducerContent().handle(null, null, true, false, aid);
114 int exitValue = Helper.rsync();
115 theLog.printDebugInfo("rsync:"+exitValue);
117 // redirecting to url
118 // should implement back to article
119 SimpleHash mergeData = new SimpleHash();
120 deliver(req, res, mergeData, commentFormDoneTemplate);
123 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
124 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
127 else throw new ServletModuleException("aid not set!");
132 * Method for delivering the form-Page for open posting
135 public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
137 SimpleHash mergeData = new SimpleHash();
138 String numOfMedia = req.getParameter("medianum");
139 if(numOfMedia==null||numOfMedia.equals("")){
143 int mediaNum = Integer.parseInt(numOfMedia);
144 SimpleList mediaFields = new SimpleList();
145 for(int i =0; i<mediaNum;i++){
146 Integer mNum = new Integer(i+1);
147 mediaFields.add(mNum.toString());
149 mergeData.put("medianum",numOfMedia);
150 mergeData.put("mediafields",mediaFields);
151 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
154 /** @todo popups missing */
156 mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
157 } catch (Exception e) {
158 theLog.printError("languagePopUpData failed");
160 deliver(req, res, mergeData, postingFormTemplate);
164 * Method for inserting an open posting into the Database and delivering
165 * the postingDone Page
168 public void insposting(HttpServletRequest req, HttpServletResponse res)
169 throws ServletModuleException
171 SimpleHash mergeData = new SimpleHash();
172 boolean setMedia=false;
175 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
177 HashMap withValues = mp.getParameters();
178 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
179 withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
180 withValues.put("is_produced", "0");
181 // op-articles are immediatly published
182 withValues.put("is_published","1");
183 // if op direct article-type == newswire
184 if (directOp.equals("yes")) withValues.put("to_article_type","1");
186 // owner is openposting user
187 withValues.put("to_publisher","1");
188 if (withValues.get("creator").toString().equals(""))
189 withValues.put("creator","Anonym");
191 // inserting content into database
192 String cid = contentModule.add(withValues);
194 String[] to_topicsArr = mp.getParameterValues("to_topic");
195 if (to_topicsArr != null && to_topicsArr.length > 0) {
197 DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
198 theLog.printError("setting content_x_topic success");
199 } catch (Exception e) {
200 theLog.printError("setting content_x_topic failed");
204 // if op contains uploaddata
207 for(Iterator it = mp.requestList.iterator(); it.hasNext();){
208 MpRequest mpReq = (MpRequest)it.next();
209 byte[] mediaData=mpReq.getMedia();
210 String fileName=mpReq.getFilename();
211 String contentType=mpReq.getContentType();
212 if (mediaData!=null && fileName!=null) {
213 HashMap mediaValues = new HashMap();
214 mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
215 mediaValues.put("to_publisher", "1"); // op user
216 mediaValues.put("to_media_folder", "7"); // op media_folder
217 mediaValues.put("is_produced", "0");
218 mediaValues.put("is_published","1");
220 String mediaTitle=(String)withValues.get("media_title"+i);
222 if (mediaTitle==null)
223 mediaTitle = (String)withValues.get("title");
224 mediaValues.put("title",mediaTitle);
226 if (fileName.toLowerCase().endsWith("rm")) {
228 //theLog.printDebugInfo("--GOT VIDEO");
229 EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
230 entVideo.setValues(mediaValues);
231 mediaId = entVideo.insert();
232 entVideo.setVideoData(mediaData);
234 else if (contentType.equals("image/jpeg") || contentType.equals("image/gif")) {
236 mediaId = imageModule.add(mediaValues);
237 EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
240 if (contentType.equals("image/jpeg")) fileType=0;
241 if (contentType.equals("image/gif")) fileType=1;
243 entImage.setImage(mediaData, fileType);
244 // inserting content and media id in table content_x_media
246 DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
247 theLog.printError("setting content_x_media success");
248 } catch (Exception e) {
249 theLog.printError("setting content_x_media failed");
252 // producing new page
254 new ProducerImages().handle(null, null, false, false, mediaId);
257 theLog.printDebugInfo("Wrong file uploaded!" + fileName);
263 // producing openpostinglist
264 new ProducerOpenPosting().handle(null,null,false,false);
265 // producing new page
266 new ProducerContent().handle(null, null, false, false,cid);
267 //if direct op producing startpage
268 if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
272 //should be configureable
273 int exitValue = Helper.rsync();
274 theLog.printDebugInfo("rsync: "+exitValue);
277 catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
278 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
279 catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
281 deliver(req, res, mergeData, postingFormDoneTemplate);