direct/indirect open posting configureable
[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 boolean         directOp;
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(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.OpenIndy.Logfile"));
49       commentFormTemplate = Configuration.getProperty("ServletModule.OpenIndy.CommentTemplate");
50       commentFormDoneTemplate = Configuration.getProperty("ServletModule.OpenIndy.CommentDoneTemplate");
51       postingFormTemplate = Configuration.getProperty("ServletModule.OpenIndy.PostingTemplate");
52       postingFormDoneTemplate = Configuration.getProperty("ServletModule.OpenIndy.PostingDoneTemplate");
53       directOp = Configuration.directOp();
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
106         // redirecting to url
107         // should implement back to article
108         SimpleHash mergeData = new SimpleHash();
109         deliver(req, res, mergeData, commentFormDoneTemplate);
110       }
111       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
112       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
113
114     }
115     else throw new ServletModuleException("aid not set!");
116
117   }
118
119   /**
120    *  Method for delivering the form-Page for open posting
121    */
122
123   public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
124   {
125     SimpleHash mergeData = new SimpleHash();
126     /** @todo popups missing */
127     try{
128       mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
129     } catch (Exception e) {
130       theLog.printError("languagePopUpData failed");
131     }
132     deliver(req, res, mergeData, postingFormTemplate);
133   }
134
135   /**
136    *  Method for inserting an open posting into the Database and delivering
137    *  the postingDone Page
138    */
139
140   public void insposting(HttpServletRequest req, HttpServletResponse res)
141     throws ServletModuleException
142   {
143     SimpleHash mergeData = new SimpleHash();
144     boolean setMedia=false;
145
146     try {
147
148       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
149       HashMap withValues = mp.getParameters();
150       byte[] mediaData=mp.getMedia();
151       String fileName=mp.getFilename();
152       
153       theLog.printDebugInfo("ContentType: "+mp.getContentType());
154       
155       // if op contains imagedata
156       String mediaId=null;
157       if (mediaData!=null && fileName!=null) {
158         HashMap mediaValues = new HashMap();
159         mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
160         mediaValues.put("to_publisher", "1"); // op user
161         mediaValues.put("to_media_folder", "7"); // op media_folder
162         mediaValues.put("is_produced", "0");
163         mediaValues.put("is_published","1");
164
165         String mediaTitle=(String)withValues.get("media_title");
166         if (mediaTitle==null)
167           mediaTitle = (String)withValues.get("title");
168         mediaValues.put("title",mediaTitle);
169
170         if (fileName.toLowerCase().endsWith("rm")) {
171           // this is video !!
172           //theLog.printDebugInfo("--GOT VIDEO");
173           EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
174           entVideo.setValues(mediaValues);
175           mediaId = entVideo.insert();
176           entVideo.setVideoData(mediaData);
177         }
178         else if (fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".gif")) {
179           // this is image !!
180           mediaId = imageModule.add(mediaValues);
181           EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
182
183           int fileType = -1;
184           if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
185           if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
186           if (fileType>=0) {
187             entImage.setImage(mediaData, fileType);
188             setMedia=true;
189           }
190           else
191             theLog.printDebugInfo("Wrong file uploaded!" + fileName);
192         }
193       }
194
195       withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
196       withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
197       withValues.put("is_produced", "0");
198       // op-articles are immediatly published
199       withValues.put("is_published","1");
200       // if op direct article-type == newswire
201       if (directOp==true) withValues.put("to_article_type","1");
202       // owner is openposting user
203       withValues.put("to_publisher","1");
204       if (withValues.get("creator").toString().equals(""))
205         withValues.put("creator","Anonym");
206
207       // inserting  content into database
208       String id = contentModule.add(withValues);
209       
210       // inserting content and media id in table content_x_media
211       try{
212         DatabaseContentToMedia.getInstance().setMedia(id,mediaId);
213         theLog.printError("setting content_x_topic success");
214       } catch (Exception e) {
215         theLog.printError("setting content_x_topic failed");
216       }
217
218
219       // producing new page
220       if(mediaId!=null){
221         new ProducerImages().handle(null, null, false, false, mediaId);
222       }
223       // producing openpostinglist
224       new ProducerOpenPosting().handle(null,null,false,false);
225       // producing new page
226       new ProducerContent().handle(null, null, false, false,id);
227       //if direct op producing startpage
228       if (directOp==true) new ProducerStartPage().handle(null,null,false,false);
229       
230
231       // sync the server
232       //should be configureable
233       int exitValue = Helper.rsync();
234
235     }
236     catch (IOException e) { throw new ServletModuleException(e.toString());}
237     catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
238     catch (ModuleException e) { throw new ServletModuleException(e.toString());}
239
240     deliver(req, res, mergeData, postingFormDoneTemplate);
241   }
242
243 }
244