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;
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 postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
53 postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
54 directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
55 mainModule = new ModuleComment(DatabaseComment.getInstance());
56 contentModule = new ModuleContent(DatabaseContent.getInstance());
57 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
58 imageModule = new ModuleImages(DatabaseImages.getInstance());
59 defaultAction="addposting";
61 catch (StorageObjectException e) {
62 theLog.printError("servletmoduleopenindy could not be initialized");
68 * Method for making a comment
71 public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
73 String aid = req.getParameter("aid"); // the article id the comment will belong to
74 if (aid!=null && !aid.equals(""))
76 SimpleHash mergeData = new SimpleHash();
78 mergeData.put("aid", aid);
79 deliver(req, res, mergeData, commentFormTemplate);
81 else throw new ServletModuleException("aid not set!");
85 * Method for inserting a comment into the Database and delivering
86 * the commentDone Page
89 public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
91 String aid = req.getParameter("to_media"); // the article id the comment will belong to
92 if (aid!=null && !aid.equals(""))
94 // ok, collecting data from form
96 HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
97 withValues.put("is_published","1");
99 // inserting into database
100 String id = mainModule.add(withValues);
102 // producing new page
103 new ProducerContent().handle(null, null, true, false, aid);
106 int exitValue = Helper.rsync();
107 theLog.printDebugInfo("rsync:"+exitValue);
109 // redirecting to url
110 // should implement back to article
111 SimpleHash mergeData = new SimpleHash();
112 deliver(req, res, mergeData, commentFormDoneTemplate);
114 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
115 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
118 else throw new ServletModuleException("aid not set!");
123 * Method for delivering the form-Page for open posting
126 public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
128 SimpleHash mergeData = new SimpleHash();
129 String numOfMedia = req.getParameter("medianum");
130 if(numOfMedia==null||numOfMedia.equals("")){
134 int mediaNum = Integer.parseInt(numOfMedia);
135 SimpleList mediaFields = new SimpleList();
136 for(int i =0; i<mediaNum;i++){
137 Integer mNum = new Integer(i+1);
138 mediaFields.add(mNum.toString());
140 mergeData.put("medianum",numOfMedia);
141 mergeData.put("mediafields",mediaFields);
142 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
145 /** @todo popups missing */
147 mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
148 } catch (Exception e) {
149 theLog.printError("languagePopUpData failed");
151 deliver(req, res, mergeData, postingFormTemplate);
155 * Method for inserting an open posting into the Database and delivering
156 * the postingDone Page
159 public void insposting(HttpServletRequest req, HttpServletResponse res)
160 throws ServletModuleException
162 SimpleHash mergeData = new SimpleHash();
163 boolean setMedia=false;
166 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
168 HashMap withValues = mp.getParameters();
169 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
170 withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
171 withValues.put("is_produced", "0");
172 // op-articles are immediatly published
173 withValues.put("is_published","1");
174 // if op direct article-type == newswire
175 if (directOp.equals("yes")) withValues.put("to_article_type","1");
177 // owner is openposting user
178 withValues.put("to_publisher","1");
179 if (withValues.get("creator").toString().equals(""))
180 withValues.put("creator","Anonym");
182 // inserting content into database
183 String cid = contentModule.add(withValues);
185 String[] to_topicsArr = mp.getParameterValues("to_topic");
186 if (to_topicsArr != null && to_topicsArr.length > 0) {
188 DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
189 theLog.printError("setting content_x_topic success");
190 } catch (Exception e) {
191 theLog.printError("setting content_x_topic failed");
195 // if op contains uploaddata
198 for(Iterator it = mp.requestList.iterator(); it.hasNext();){
199 MpRequest mpReq = (MpRequest)it.next();
200 byte[] mediaData=mpReq.getMedia();
201 String fileName=mpReq.getFilename();
202 String contentType=mpReq.getContentType();
203 if (mediaData!=null && fileName!=null) {
204 HashMap mediaValues = new HashMap();
205 mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
206 mediaValues.put("to_publisher", "1"); // op user
207 mediaValues.put("to_media_folder", "7"); // op media_folder
208 mediaValues.put("is_produced", "0");
209 mediaValues.put("is_published","1");
211 String mediaTitle=(String)withValues.get("media_title"+i);
213 if (mediaTitle==null)
214 mediaTitle = (String)withValues.get("title");
215 mediaValues.put("title",mediaTitle);
217 if (fileName.toLowerCase().endsWith("rm")) {
219 //theLog.printDebugInfo("--GOT VIDEO");
220 EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
221 entVideo.setValues(mediaValues);
222 mediaId = entVideo.insert();
223 entVideo.setVideoData(mediaData);
225 else if (contentType.equals("image/jpeg") || contentType.equals("image/gif")) {
227 mediaId = imageModule.add(mediaValues);
228 EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
231 if (contentType.equals("image/jpeg")) fileType=0;
232 if (contentType.equals("image/gif")) fileType=1;
234 entImage.setImage(mediaData, fileType);
235 // inserting content and media id in table content_x_media
237 DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
238 theLog.printError("setting content_x_media success");
239 } catch (Exception e) {
240 theLog.printError("setting content_x_media failed");
243 // producing new page
245 new ProducerImages().handle(null, null, false, false, mediaId);
248 theLog.printDebugInfo("Wrong file uploaded!" + fileName);
254 // producing openpostinglist
255 new ProducerOpenPosting().handle(null,null,false,false);
256 // producing new page
257 new ProducerContent().handle(null, null, false, false,cid);
258 //if direct op producing startpage
259 if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
263 //should be configureable
264 int exitValue = Helper.rsync();
265 theLog.printDebugInfo("rsync: "+exitValue);
268 catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
269 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
270 catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
272 deliver(req, res, mergeData, postingFormDoneTemplate);