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