6452a7eb5f39f9f0c230417455fd08962f3e1dbd
[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 webdb.servlet.*;
14 import webdb.module.*;
15 import webdb.misc.*;
16 import webdb.entity.*;
17 import webdb.storage.*;
18
19 import mir.entity.*;
20 import mir.storage.*;
21 import mir.module.*;
22 import mir.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                 deliver(req, res, mergeData, postingFormTemplate);
127         }
128
129         /**
130          *  Method for inserting an open posting into the Database and delivering
131          *  the postingDone Page
132          */
133
134         public void insposting(HttpServletRequest req, HttpServletResponse res)
135                 throws ServletModuleException
136         {
137                 SimpleHash mergeData = new SimpleHash();
138
139                 try {
140
141                         WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
142                         HashMap withValues = mp.getParameters();
143                         byte[] mediaData=mp.getMedia();
144                         String fileName=mp.getFilename();
145
146                         // if op contains imagedata
147                         String mediaId=null;
148                         if (mediaData!=null && fileName!=null) {
149                                 HashMap mediaValues = new HashMap();
150                                 mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
151                                 mediaValues.put("to_publisher", "1"); // op user
152                                 mediaValues.put("to_media_folder", "7"); // op media_folder
153                                 mediaValues.put("is_produced", "0");
154                                 mediaValues.put("is_published","1");
155
156                                 String mediaTitle=(String)withValues.get("media_title");
157                                 if (mediaTitle==null)
158                                         mediaTitle = (String)withValues.get("title");
159                                 mediaValues.put("title",mediaTitle);
160
161                                 if (fileName.toLowerCase().endsWith("rm")) {
162                                         // this is video !!
163                                         //theLog.printDebugInfo("--GOT VIDEO");
164                                         EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
165                                         entVideo.setValues(mediaValues);
166                                         mediaId = entVideo.insert();
167                                         entVideo.setVideoData(mediaData);
168                                 }
169                                 else if (fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".gif")) {
170                                         // this is image !!
171                                         mediaId = imageModule.add(mediaValues);
172                                         EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
173
174                                         int fileType = -1;
175                                         if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
176                                         if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
177                                         if (fileType>=0) {
178                                                 entImage.setImage(mediaData, fileType);
179                                                 withValues.put("to_media",mediaId);
180                                         }
181                                         else
182                                                 theLog.printDebugInfo("Wrong file uploaded!" + fileName);
183                                 }
184                         }
185
186                         withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
187                         withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
188                         withValues.put("is_produced", "0");
189                         // op-articles are immediatly published
190                         withValues.put("is_published","1");
191                         // owner is openposting user
192                         withValues.put("to_publisher","1");
193       if (withValues.get("creator").toString().equals(""))
194                                 withValues.put("creator","Anonym");
195
196                         // inserting  content into database
197                         String id = contentModule.add(withValues);
198
199
200                         // producing new page
201                         if(mediaId!=null){
202                                 new ProducerImages().handle(null, null, false, false, mediaId);
203                         }
204                         // producing openpostinglist
205                         new ProducerOpenPosting().handle(null,null,false,false);
206                         // producing new page
207                         new ProducerContent().handle(null, null, false, false,id);
208
209                         // sync the server
210                         int exitValue = Helper.rsync();
211
212                 }
213                 catch (IOException e) { throw new ServletModuleException(e.toString());}
214                 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
215                 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
216
217                 deliver(req, res, mergeData, postingFormDoneTemplate);
218         }
219
220 }
221