cc4e9267417c51ca49b592dc5c24767e6a4ba652
[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 String          directOp ="yes";
41
42   // Singelton / Kontruktor
43   private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
44   public static ServletModule getInstance() { return instance; }
45
46   private ServletModuleOpenIndy() {
47     try {
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";
58     }
59     catch (StorageObjectException e) {
60         theLog.printError("servletmoduleopenindy could not be initialized");
61     }
62   }
63
64
65   /**
66    *  Method for making a comment
67    */
68
69   public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
70   {
71     String aid = req.getParameter("aid"); // the article id the comment will belong to
72     if (aid!=null && !aid.equals(""))
73     {
74       SimpleHash mergeData = new SimpleHash();
75       // ok, article
76       mergeData.put("aid", aid);
77       deliver(req, res, mergeData, commentFormTemplate);
78     }
79     else throw new ServletModuleException("aid not set!");
80   }
81
82   /**
83    *  Method for inserting a comment into the Database and delivering
84    *  the commentDone Page
85    */
86
87   public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
88   {
89     String aid = req.getParameter("to_media"); // the article id the comment will belong to
90     if (aid!=null && !aid.equals(""))
91     {
92       // ok, collecting data from form
93       try {
94         HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
95         withValues.put("is_published","1");
96
97         // inserting into database
98         String id = mainModule.add(withValues);
99
100         // producing new page
101         new ProducerContent().handle(null, null, true, false, aid);
102
103         // sync the server
104         int exitValue = Helper.rsync();
105         theLog.printDebugInfo("rsync:"+exitValue);
106
107         // redirecting to url
108         // should implement back to article
109         SimpleHash mergeData = new SimpleHash();
110         deliver(req, res, mergeData, commentFormDoneTemplate);
111       }
112       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
113       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
114
115     }
116     else throw new ServletModuleException("aid not set!");
117
118   }
119
120   /**
121    *  Method for delivering the form-Page for open posting
122    */
123
124   public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
125   {
126     SimpleHash mergeData = new SimpleHash();
127     String numOfMedia = req.getParameter("medianum");
128     if(numOfMedia==null||numOfMedia.equals("")){
129       numOfMedia="1";
130     }
131     
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());
137     }
138     mergeData.put("medianum",numOfMedia);
139     mergeData.put("mediafields",mediaFields);
140     
141     
142     /** @todo popups missing */
143     try{
144       mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
145     } catch (Exception e) {
146       theLog.printError("languagePopUpData failed");
147     }
148     deliver(req, res, mergeData, postingFormTemplate);
149   }
150
151   /**
152    *  Method for inserting an open posting into the Database and delivering
153    *  the postingDone Page
154    */
155
156   public void insposting(HttpServletRequest req, HttpServletResponse res)
157     throws ServletModuleException
158   {
159     SimpleHash mergeData = new SimpleHash();
160     boolean setMedia=false;
161
162     try {
163       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
164           
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");
173       
174       // owner is openposting user
175       withValues.put("to_publisher","1");
176       if (withValues.get("creator").toString().equals(""))
177         withValues.put("creator","Anonym");
178
179       // inserting  content into database
180       String cid = contentModule.add(withValues);
181       
182       // if op contains uploaddata
183       String mediaId=null;
184       int i=1;
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");
197   
198           String mediaTitle=(String)withValues.get("media_title"+i);
199           i++;
200           if (mediaTitle==null)
201             mediaTitle = (String)withValues.get("title");
202           mediaValues.put("title",mediaTitle);
203   
204           if (fileName.toLowerCase().endsWith("rm")) {
205             // this is video !!
206             //theLog.printDebugInfo("--GOT VIDEO");
207             EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
208             entVideo.setValues(mediaValues);
209             mediaId = entVideo.insert();
210             entVideo.setVideoData(mediaData);
211           }
212           else if (contentType.equals("image/jpeg") || contentType.equals("image/gif")) {
213             // this is image !!
214             mediaId = imageModule.add(mediaValues);
215             EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
216   
217             int fileType = -1;
218             if (contentType.equals("image/jpeg")) fileType=0;
219             if (contentType.equals("image/gif")) fileType=1;
220             if (fileType>=0) {
221               entImage.setImage(mediaData, fileType);
222               // inserting content and media id in table content_x_media
223               try{
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");
228               }
229               
230               // producing new page
231               if(mediaId!=null){
232                 new ProducerImages().handle(null, null, false, false, mediaId);
233               }
234             } else {
235               theLog.printDebugInfo("Wrong file uploaded!" + fileName);
236             }
237           }
238         }
239       }
240
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);
247       
248
249       // sync the server
250       //should be configureable
251       int exitValue = Helper.rsync();
252       theLog.printDebugInfo("rsync: "+exitValue);
253
254     }
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());}
258
259     deliver(req, res, mergeData, postingFormDoneTemplate);
260   }
261
262 }
263