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 String directOp ="yes";
42 // Singelton / Kontruktor
43 private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
44 public static ServletModule getInstance() { return instance; }
46 private ServletModuleOpenIndy() {
48 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
49 commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
50 commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
51 postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
52 postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
53 directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
54 mainModule = new ModuleComment(DatabaseComment.getInstance());
55 contentModule = new ModuleContent(DatabaseContent.getInstance());
56 imageModule = new ModuleImages(DatabaseImages.getInstance());
57 defaultAction="addposting";
59 catch (StorageObjectException e) {
60 theLog.printError("servletmoduleopenindy could not be initialized");
66 * Method for making a comment
69 public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
71 String aid = req.getParameter("aid"); // the article id the comment will belong to
72 if (aid!=null && !aid.equals(""))
74 SimpleHash mergeData = new SimpleHash();
76 mergeData.put("aid", aid);
77 deliver(req, res, mergeData, commentFormTemplate);
79 else throw new ServletModuleException("aid not set!");
83 * Method for inserting a comment into the Database and delivering
84 * the commentDone Page
87 public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
89 String aid = req.getParameter("to_media"); // the article id the comment will belong to
90 if (aid!=null && !aid.equals(""))
92 // ok, collecting data from form
94 HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
95 withValues.put("is_published","1");
97 // inserting into database
98 String id = mainModule.add(withValues);
100 // producing new page
101 new ProducerContent().handle(null, null, true, false, aid);
104 int exitValue = Helper.rsync();
105 theLog.printDebugInfo("rsync:"+exitValue);
107 // redirecting to url
108 // should implement back to article
109 SimpleHash mergeData = new SimpleHash();
110 deliver(req, res, mergeData, commentFormDoneTemplate);
112 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
113 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
116 else throw new ServletModuleException("aid not set!");
121 * Method for delivering the form-Page for open posting
124 public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
126 SimpleHash mergeData = new SimpleHash();
127 String numOfMedia = req.getParameter("medianum");
128 if(numOfMedia==null||numOfMedia.equals("")){
132 int mediaNum = Integer.parseInt(numOfMedia);
133 SimpleList mediaFields = new SimpleList();
134 for(int i =0; i<mediaNum;i++){
135 Integer mNum = new Integer(i+1);
136 mediaFields.add(mNum.toString());
138 mergeData.put("medianum",numOfMedia);
139 mergeData.put("mediafields",mediaFields);
142 /** @todo popups missing */
144 mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
145 } catch (Exception e) {
146 theLog.printError("languagePopUpData failed");
148 deliver(req, res, mergeData, postingFormTemplate);
152 * Method for inserting an open posting into the Database and delivering
153 * the postingDone Page
156 public void insposting(HttpServletRequest req, HttpServletResponse res)
157 throws ServletModuleException
159 SimpleHash mergeData = new SimpleHash();
160 boolean setMedia=false;
163 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
165 HashMap withValues = mp.getParameters();
166 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
167 withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
168 withValues.put("is_produced", "0");
169 // op-articles are immediatly published
170 withValues.put("is_published","1");
171 // if op direct article-type == newswire
172 if (directOp.equals("yes")) withValues.put("to_article_type","1");
174 // owner is openposting user
175 withValues.put("to_publisher","1");
176 if (withValues.get("creator").toString().equals(""))
177 withValues.put("creator","Anonym");
179 // inserting content into database
180 String cid = contentModule.add(withValues);
182 // if op contains uploaddata
185 for(Iterator it = mp.requestList.iterator(); it.hasNext();){
186 MpRequest mpReq = (MpRequest)it.next();
187 byte[] mediaData=mpReq.getMedia();
188 String fileName=mpReq.getFilename();
189 String contentType=mpReq.getContentType();
190 if (mediaData!=null && fileName!=null) {
191 HashMap mediaValues = new HashMap();
192 mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
193 mediaValues.put("to_publisher", "1"); // op user
194 mediaValues.put("to_media_folder", "7"); // op media_folder
195 mediaValues.put("is_produced", "0");
196 mediaValues.put("is_published","1");
198 String mediaTitle=(String)withValues.get("media_title"+i);
200 if (mediaTitle==null)
201 mediaTitle = (String)withValues.get("title");
202 mediaValues.put("title",mediaTitle);
204 if (fileName.toLowerCase().endsWith("rm")) {
206 //theLog.printDebugInfo("--GOT VIDEO");
207 EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
208 entVideo.setValues(mediaValues);
209 mediaId = entVideo.insert();
210 entVideo.setVideoData(mediaData);
212 else if (contentType.equals("image/jpeg") || contentType.equals("image/gif")) {
214 mediaId = imageModule.add(mediaValues);
215 EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
218 if (contentType.equals("image/jpeg")) fileType=0;
219 if (contentType.equals("image/gif")) fileType=1;
221 entImage.setImage(mediaData, fileType);
222 // inserting content and media id in table content_x_media
224 DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
225 theLog.printError("setting content_x_topic success");
226 } catch (Exception e) {
227 theLog.printError("setting content_x_topic failed");
230 // producing new page
232 new ProducerImages().handle(null, null, false, false, mediaId);
235 theLog.printDebugInfo("Wrong file uploaded!" + fileName);
241 // producing openpostinglist
242 new ProducerOpenPosting().handle(null,null,false,false);
243 // producing new page
244 new ProducerContent().handle(null, null, false, false,cid);
245 //if direct op producing startpage
246 if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
250 //should be configureable
251 int exitValue = Helper.rsync();
252 theLog.printDebugInfo("rsync: "+exitValue);
255 catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
256 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
257 catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
259 deliver(req, res, mergeData, postingFormDoneTemplate);