Modified config system:
[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       //directOp="yes";
55       mainModule = new ModuleComment(DatabaseComment.getInstance());
56       contentModule = new ModuleContent(DatabaseContent.getInstance());
57       imageModule = new ModuleImages(DatabaseImages.getInstance());
58       defaultAction="addposting";
59     }
60     catch (StorageObjectException e) {
61         theLog.printError("servletmoduleopenindy could not be initialized");
62     }
63   }
64
65
66   /**
67    *  Method for making a comment
68    */
69
70   public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
71   {
72     String aid = req.getParameter("aid"); // the article id the comment will belong to
73     if (aid!=null && !aid.equals(""))
74     {
75       SimpleHash mergeData = new SimpleHash();
76       // ok, article
77       mergeData.put("aid", aid);
78       deliver(req, res, mergeData, commentFormTemplate);
79     }
80     else throw new ServletModuleException("aid not set!");
81   }
82
83   /**
84    *  Method for inserting a comment into the Database and delivering
85    *  the commentDone Page
86    */
87
88   public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
89   {
90     String aid = req.getParameter("to_media"); // the article id the comment will belong to
91     if (aid!=null && !aid.equals(""))
92     {
93       // ok, collecting data from form
94       try {
95         HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
96         withValues.put("is_published","1");
97
98         // inserting into database
99         String id = mainModule.add(withValues);
100
101         // producing new page
102         new ProducerContent().handle(null, null, true, false, aid);
103
104         // sync the server
105         int exitValue = Helper.rsync();
106                                 theLog.printDebugInfo("rsync:"+exitValue);
107
108         // redirecting to url
109         // should implement back to article
110         SimpleHash mergeData = new SimpleHash();
111         deliver(req, res, mergeData, commentFormDoneTemplate);
112       }
113       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
114       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
115
116     }
117     else throw new ServletModuleException("aid not set!");
118
119   }
120
121   /**
122    *  Method for delivering the form-Page for open posting
123    */
124
125   public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
126   {
127     SimpleHash mergeData = new SimpleHash();
128     /** @todo popups missing */
129     try{
130       mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
131     } catch (Exception e) {
132       theLog.printError("languagePopUpData failed");
133     }
134     deliver(req, res, mergeData, postingFormTemplate);
135   }
136
137   /**
138    *  Method for inserting an open posting into the Database and delivering
139    *  the postingDone Page
140    */
141
142   public void insposting(HttpServletRequest req, HttpServletResponse res)
143     throws ServletModuleException
144   {
145     SimpleHash mergeData = new SimpleHash();
146     boolean setMedia=false;
147
148     try {
149
150       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
151       HashMap withValues = mp.getParameters();
152       byte[] mediaData=mp.getMedia();
153       String fileName=mp.getFilename();
154       
155       theLog.printDebugInfo("ContentType: "+mp.getContentType());
156       
157       // if op contains imagedata
158       String mediaId=null;
159       if (mediaData!=null && fileName!=null) {
160         HashMap mediaValues = new HashMap();
161         mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
162         mediaValues.put("to_publisher", "1"); // op user
163         mediaValues.put("to_media_folder", "7"); // op media_folder
164         mediaValues.put("is_produced", "0");
165         mediaValues.put("is_published","1");
166
167         String mediaTitle=(String)withValues.get("media_title");
168         if (mediaTitle==null)
169           mediaTitle = (String)withValues.get("title");
170         mediaValues.put("title",mediaTitle);
171
172         if (fileName.toLowerCase().endsWith("rm")) {
173           // this is video !!
174           //theLog.printDebugInfo("--GOT VIDEO");
175           EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
176           entVideo.setValues(mediaValues);
177           mediaId = entVideo.insert();
178           entVideo.setVideoData(mediaData);
179         }
180         else if (fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".gif")) {
181           // this is image !!
182           mediaId = imageModule.add(mediaValues);
183           EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
184
185           int fileType = -1;
186           if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
187           if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
188           if (fileType>=0) {
189             entImage.setImage(mediaData, fileType);
190             setMedia=true;
191           }
192           else
193             theLog.printDebugInfo("Wrong file uploaded!" + fileName);
194         }
195       }
196
197       withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
198       withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
199       withValues.put("is_produced", "0");
200       // op-articles are immediatly published
201       withValues.put("is_published","1");
202       // if op direct article-type == newswire
203       if (directOp.equals("yes")) withValues.put("to_article_type","1");
204       theLog.printDebugInfo("direct op: "+directOp);
205       
206       // owner is openposting user
207       withValues.put("to_publisher","1");
208       if (withValues.get("creator").toString().equals(""))
209         withValues.put("creator","Anonym");
210
211       // inserting  content into database
212       String id = contentModule.add(withValues);
213       
214       // inserting content and media id in table content_x_media
215       try{
216         DatabaseContentToMedia.getInstance().setMedia(id,mediaId);
217         theLog.printError("setting content_x_topic success");
218       } catch (Exception e) {
219         theLog.printError("setting content_x_topic failed");
220       }
221
222
223       // producing new page
224       if(mediaId!=null){
225         new ProducerImages().handle(null, null, false, false, mediaId);
226       }
227       // producing openpostinglist
228       new ProducerOpenPosting().handle(null,null,false,false);
229       // producing new page
230       new ProducerContent().handle(null, null, false, false,id);
231       //if direct op producing startpage
232       if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
233       
234
235       // sync the server
236       //should be configureable
237       int exitValue = Helper.rsync();
238                         theLog.printDebugInfo("rsync: "+exitValue);
239
240     }
241     catch (IOException e) { throw new ServletModuleException(e.toString());}
242     catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
243     catch (ModuleException e) { throw new ServletModuleException(e.toString());}
244
245     deliver(req, res, mergeData, postingFormDoneTemplate);
246   }
247
248 }
249