7322c24600e58e05aa8c29d7b9b81e9007a01263
[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;
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       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";
60     }
61     catch (StorageObjectException e) {
62         theLog.printError("servletmoduleopenindy could not be initialized");
63     }
64   }
65
66
67   /**
68    *  Method for making a comment
69    */
70
71   public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
72   {
73     String aid = req.getParameter("aid"); // the article id the comment will belong to
74     if (aid!=null && !aid.equals(""))
75     {
76       SimpleHash mergeData = new SimpleHash();
77       // ok, article
78       mergeData.put("aid", aid);
79       deliver(req, res, mergeData, commentFormTemplate);
80     }
81     else throw new ServletModuleException("aid not set!");
82   }
83
84   /**
85    *  Method for inserting a comment into the Database and delivering
86    *  the commentDone Page
87    */
88
89   public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
90   {
91     String aid = req.getParameter("to_media"); // the article id the comment will belong to
92     if (aid!=null && !aid.equals(""))
93     {
94       // ok, collecting data from form
95       try {
96         HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
97         withValues.put("is_published","1");
98
99         // inserting into database
100         String id = mainModule.add(withValues);
101
102         // producing new page
103         new ProducerContent().handle(null, null, true, false, aid);
104
105         // sync the server
106         int exitValue = Helper.rsync();
107         theLog.printDebugInfo("rsync:"+exitValue);
108
109         // redirecting to url
110         // should implement back to article
111         SimpleHash mergeData = new SimpleHash();
112         deliver(req, res, mergeData, commentFormDoneTemplate);
113       }
114       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
115       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
116
117     }
118     else throw new ServletModuleException("aid not set!");
119
120   }
121
122   /**
123    *  Method for delivering the form-Page for open posting
124    */
125
126   public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
127   {
128     SimpleHash mergeData = new SimpleHash();
129     String numOfMedia = req.getParameter("medianum");
130     if(numOfMedia==null||numOfMedia.equals("")){
131       numOfMedia="1";
132     }
133     
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());
139     }
140     mergeData.put("medianum",numOfMedia);
141     mergeData.put("mediafields",mediaFields);
142     mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
143     
144     
145     /** @todo popups missing */
146     try{
147       mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
148     } catch (Exception e) {
149       theLog.printError("languagePopUpData failed");
150     }
151     deliver(req, res, mergeData, postingFormTemplate);
152   }
153
154   /**
155    *  Method for inserting an open posting into the Database and delivering
156    *  the postingDone Page
157    */
158
159   public void insposting(HttpServletRequest req, HttpServletResponse res)
160     throws ServletModuleException
161   {
162     SimpleHash mergeData = new SimpleHash();
163     boolean setMedia=false;
164
165     try {
166       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
167           
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");
176       
177       // owner is openposting user
178       withValues.put("to_publisher","1");
179       if (withValues.get("creator").toString().equals(""))
180         withValues.put("creator","Anonym");
181
182       // inserting  content into database
183       String cid = contentModule.add(withValues);
184
185       String[] to_topicsArr = mp.getParameterValues("to_topic"); 
186       if (to_topicsArr != null && to_topicsArr.length > 0) {
187         try{
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");
192         } //end try
193       } //end if
194         
195       // if op contains uploaddata
196       String mediaId=null;
197       int i=1;
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");
210   
211           String mediaTitle=(String)withValues.get("media_title"+i);
212           i++;
213           if (mediaTitle==null)
214             mediaTitle = (String)withValues.get("title");
215           mediaValues.put("title",mediaTitle);
216   
217           if (fileName.toLowerCase().endsWith("rm")) {
218             // this is video !!
219             //theLog.printDebugInfo("--GOT VIDEO");
220             EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
221             entVideo.setValues(mediaValues);
222             mediaId = entVideo.insert();
223             entVideo.setVideoData(mediaData);
224           }
225           else if (contentType.equals("image/jpeg") || contentType.equals("image/gif")) {
226             // this is image !!
227             mediaId = imageModule.add(mediaValues);
228             EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
229   
230             int fileType = -1;
231             if (contentType.equals("image/jpeg")) fileType=0;
232             if (contentType.equals("image/gif")) fileType=1;
233             if (fileType>=0) {
234               entImage.setImage(mediaData, fileType);
235               // inserting content and media id in table content_x_media
236               try{
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");
241               }
242               
243               // producing new page
244               if(mediaId!=null){
245                 new ProducerImages().handle(null, null, false, false, mediaId);
246               }
247             } else {
248               theLog.printDebugInfo("Wrong file uploaded!" + fileName);
249             }
250           }
251         }
252       }
253
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);
260       
261
262       // sync the server
263       //should be configureable
264       int exitValue = Helper.rsync();
265       theLog.printDebugInfo("rsync: "+exitValue);
266
267     }
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());}
271
272     deliver(req, res, mergeData, postingFormDoneTemplate);
273   }
274
275 }
276