Java base dupecheck deinstalled because the dupecheck is now implemented
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
1 package mircoders.servlet;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8
9 import freemarker.template.*;
10 import com.oreilly.servlet.multipart.*;
11 import com.oreilly.servlet.*;
12
13 import mir.servlet.*;
14 import mir.module.*;
15 import mir.misc.*;
16 import mir.entity.*;
17 import mir.storage.*;
18
19 import mircoders.entity.*;
20 import mircoders.storage.*;
21 import mircoders.module.*;
22 import mircoders.producer.*;
23
24 /*
25  *  ServletModuleOpenIndy -
26  *   is the open-access-servlet, which is responsible for
27  *    adding comments to articles &
28  *    open-postings to the newswire
29  *
30  * @author RK
31  */
32
33 public class ServletModuleOpenIndy extends ServletModule
34 {
35
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";
42
43   // Singelton / Kontruktor
44   private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
45   public static ServletModule getInstance() { return instance; }
46
47   private ServletModuleOpenIndy() {
48     try {
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";
61     }
62     catch (StorageObjectException e) {
63         theLog.printError("servletmoduleopenindy could not be initialized");
64     }
65   }
66
67
68   /**
69    *  Method for making a comment
70    */
71
72   public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
73   {
74     String aid = req.getParameter("aid"); // the article id the comment will belong to
75     if (aid!=null && !aid.equals(""))
76     {
77       SimpleHash mergeData = new SimpleHash();
78       // ok, article
79       mergeData.put("aid", aid);
80       deliver(req, res, mergeData, commentFormTemplate);
81     }
82     else throw new ServletModuleException("aid not set!");
83   }
84
85   /**
86    *  Method for inserting a comment into the Database and delivering
87    *  the commentDone Page
88    */
89
90   public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
91   {
92     String aid = req.getParameter("to_media"); // the article id the comment will belong to
93     if (aid!=null && !aid.equals(""))
94     {
95       // ok, collecting data from form
96       try {
97         HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
98         withValues.put("is_published","1");
99
100                 // Dupe detection
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);
105 //              } else {
106
107                 // inserting into database
108                 String id = mainModule.add(withValues);
109                 
110                 // producing new page
111                 new ProducerContent().handle(null, null, true, false, aid);
112
113                 // sync the server
114                 int exitValue = Helper.rsync();
115                 theLog.printDebugInfo("rsync:"+exitValue);
116
117                 // redirecting to url
118                 // should implement back to article
119                 SimpleHash mergeData = new SimpleHash();
120                 deliver(req, res, mergeData, commentFormDoneTemplate);
121 //              }
122       }
123       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
124       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
125
126     }
127     else throw new ServletModuleException("aid not set!");
128
129   }
130
131   /**
132    *  Method for delivering the form-Page for open posting
133    */
134
135   public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
136   {
137     SimpleHash mergeData = new SimpleHash();
138     String numOfMedia = req.getParameter("medianum");
139     if(numOfMedia==null||numOfMedia.equals("")){
140       numOfMedia="1";
141     }
142     
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());
148     }
149     mergeData.put("medianum",numOfMedia);
150     mergeData.put("mediafields",mediaFields);
151     mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
152     
153     
154     /** @todo popups missing */
155     try{
156       mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
157     } catch (Exception e) {
158       theLog.printError("languagePopUpData failed");
159     }
160     deliver(req, res, mergeData, postingFormTemplate);
161   }
162
163   /**
164    *  Method for inserting an open posting into the Database and delivering
165    *  the postingDone Page
166    */
167
168   public void insposting(HttpServletRequest req, HttpServletResponse res)
169     throws ServletModuleException
170   {
171     SimpleHash mergeData = new SimpleHash();
172     boolean setMedia=false;
173
174     try {
175       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
176           
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");
185       
186       // owner is openposting user
187       withValues.put("to_publisher","1");
188       if (withValues.get("creator").toString().equals(""))
189         withValues.put("creator","Anonym");
190
191       // inserting  content into database
192       String cid = contentModule.add(withValues);
193
194       String[] to_topicsArr = mp.getParameterValues("to_topic"); 
195       if (to_topicsArr != null && to_topicsArr.length > 0) {
196         try{
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");
201         } //end try
202       } //end if
203         
204       // if op contains uploaddata
205       String mediaId=null;
206       int i=1;
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");
219   
220           String mediaTitle=(String)withValues.get("media_title"+i);
221           i++;
222           if (mediaTitle==null)
223             mediaTitle = (String)withValues.get("title");
224           mediaValues.put("title",mediaTitle);
225   
226           if (fileName.toLowerCase().endsWith("rm")) {
227             // this is video !!
228             //theLog.printDebugInfo("--GOT VIDEO");
229             EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
230             entVideo.setValues(mediaValues);
231             mediaId = entVideo.insert();
232             entVideo.setVideoData(mediaData);
233           }
234           else if (contentType.equals("image/jpeg") || contentType.equals("image/gif")) {
235             // this is image !!
236             mediaId = imageModule.add(mediaValues);
237             EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
238   
239             int fileType = -1;
240             if (contentType.equals("image/jpeg")) fileType=0;
241             if (contentType.equals("image/gif")) fileType=1;
242             if (fileType>=0) {
243               entImage.setImage(mediaData, fileType);
244               // inserting content and media id in table content_x_media
245               try{
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");
250               }
251               
252               // producing new page
253               if(mediaId!=null){
254                 new ProducerImages().handle(null, null, false, false, mediaId);
255               }
256             } else {
257               theLog.printDebugInfo("Wrong file uploaded!" + fileName);
258             }
259           }
260         }
261       }
262
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);
269       
270
271       // sync the server
272       //should be configureable
273       int exitValue = Helper.rsync();
274       theLog.printDebugInfo("rsync: "+exitValue);
275
276     }
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());}
280
281     deliver(req, res, mergeData, postingFormDoneTemplate);
282   }
283
284 }
285