mass update mir codeswitch
[mir.git] / source / mircoders / servlet / ServletModuleImages.java
1 package mircoders.servlet;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6 import java.net.*;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9
10 import freemarker.template.*;
11 import com.oreilly.servlet.multipart.*;
12 import com.oreilly.servlet.*;
13
14 import mir.servlet.*;
15 import mir.module.*;
16 import mir.misc.*;
17 import mir.entity.*;
18 import mir.storage.*;
19
20 import mircoders.entity.*;
21 import mircoders.storage.*;
22 import mircoders.module.*;
23
24 /*
25  *  ServletModuleBilder -
26  *  liefert HTML fuer Bilder
27  *
28  *
29  * @author RK
30  */
31
32 public class ServletModuleImages extends mir.servlet.ServletModule
33 {
34
35         private static ModuleMediafolder mediafolderModule;
36         private static DatabaseRights dbRights;
37         private static DatabaseImageFormat dbImageFormat;
38         private static DatabaseImageType dbImageType;
39         private static DatabaseImageColor dbImageColor;
40         private static DatabaseImageLayout dbImageLayout;
41
42         // Singelton / Kontruktor
43         private static ServletModuleImages instance = new ServletModuleImages();
44         public static ServletModule getInstance() { return instance; }
45
46
47         private ServletModuleImages() {
48                 theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.Bilder.Logfile"));
49                 templateListString = Configuration.getProperty("ServletModule.Bilder.ListTemplate");
50                 templateObjektString = Configuration.getProperty("ServletModule.Bilder.ObjektTemplate");
51                 templateConfirmString = Configuration.getProperty("ServletModule.Bilder.ConfirmTemplate");
52                 try {
53                         mainModule = new ModuleImages(DatabaseImages.getInstance());
54                         mediafolderModule = new ModuleMediafolder(DatabaseMediafolder.getInstance());
55                         dbRights = DatabaseRights.getInstance();
56                         dbImageFormat = DatabaseImageFormat.getInstance();
57                         dbImageColor = DatabaseImageColor.getInstance();
58                         dbImageType = DatabaseImageType.getInstance();
59                         dbImageLayout = DatabaseImageLayout.getInstance();
60                 }
61                 catch (StorageObjectException e) {
62                         theLog.printDebugInfo("servletmodulebilder konnte nicht initialisiert werden");
63                 }
64         }
65
66
67         public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
68         {
69                 try {
70                         WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
71                         HashMap parameters = mp.getParameters();
72                         byte[] imageData=mp.getMedia();
73                         String fileName=mp.getFilename();
74
75                         EntityUsers   user = _getUser(req);
76                         parameters.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
77                         parameters.put("to_publisher", user.getId());
78                         parameters.put("is_produced", "0");
79                         if (!parameters.containsKey("is_published"))
80                                 parameters.put("is_published","0");
81
82                         String id = mainModule.add(parameters);
83                         EntityImage entImage = (EntityImage)mainModule.getById(id);
84
85                         if (imageData!=null && fileName!=null) {
86                                 int fileType = -1;
87                                 if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
88                                 if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
89                                 if (fileType>=0)
90                                         entImage.setImage(imageData, fileType);
91                                 else
92                                         theLog.printError("Wrong file uploaded!");
93                         }
94                         _edit(id, req, res);
95                 }
96                 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
97                 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
98
99         }
100
101         public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
102         {
103
104                 try {
105                         WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
106                         HashMap parameters = mp.getParameters();
107                         byte[] imageData=mp.getMedia();
108                         String fileName=mp.getFilename();
109
110                         EntityUsers   user = _getUser(req);
111                         parameters.put("to_publisher", user.getId());
112                         parameters.put("is_produced", "0");
113                         if (!parameters.containsKey("is_published"))
114                                 parameters.put("is_published","0");
115
116                         String id = mainModule.set(parameters);
117                         EntityImage entImage = (EntityImage)mainModule.getById(id);
118
119                         if (imageData!=null && fileName!=null) {
120                                 int fileType = -1;
121                                 if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
122                                 if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
123                                 if (fileType>=0)
124                                         entImage.setImage(imageData, fileType);
125                                 else
126                                         theLog.printError("Wrong file uploaded!");
127                         }
128                         _edit(id, req, res);
129                 }
130                 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
131                 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
132
133         }
134
135
136         public void showimg(HttpServletRequest req, HttpServletResponse res)
137                 throws ServletModuleException
138         {
139                 String idParam = req.getParameter("id");
140                 if (idParam!=null && !idParam.equals("")) {
141                         try {
142                                 EntityImage entImage =(EntityImage)mainModule.getById(idParam);
143                                 res.setContentType("image/jpeg"); // testweise
144                                 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
145
146                                 byte[] outbytes = entImage.getImage();
147                                 out.write(outbytes);
148                                 out.close();
149                         }
150
151                         catch (IOException e) {throw new ServletModuleException(e.toString());}
152                         catch (ModuleException e) {throw new ServletModuleException(e.toString());}
153                         catch (Exception e) {throw new ServletModuleException(e.toString());}
154                 }
155                 else theLog.printDebugInfo("id nicht angeben.");
156                 // darf keine exception werfen
157         }
158
159         public void showicon(HttpServletRequest req, HttpServletResponse res)
160                 throws ServletModuleException
161         {
162                 String idParam = req.getParameter("id");
163                 if (idParam!=null && !idParam.equals("")) {
164                         try {
165                                 EntityImage entImage =(EntityImage)mainModule.getById(idParam);
166                                 res.setContentType("image/jpeg"); // testweise
167                                 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
168
169                                 byte[] outbytes = entImage.getIcon();
170                                 out.write(outbytes);
171                                 out.close();
172                         }
173
174                         catch (IOException e) {throw new ServletModuleException(e.toString());}
175                         catch (ModuleException e) {throw new ServletModuleException(e.toString());}
176                         catch (Exception e) {throw new ServletModuleException(e.toString());}
177                 }
178                 else throw new ServletModuleException("id nicht angeben.");
179         }
180
181         public void list(HttpServletRequest req, HttpServletResponse res)
182                 throws ServletModuleException
183         {
184                         // Parameter auswerten
185                         SimpleHash mergeData = new SimpleHash();
186                         String query_text = req.getParameter("query_text");
187                         mergeData.put("query_text",query_text);
188                         if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
189                         String query_field = req.getParameter("query_field");
190                         mergeData.put("query_field",query_field);
191                         String query_is_published = req.getParameter("query_is_published");
192                         mergeData.put("query_is_published",query_is_published);
193                         String query_media_folder = req.getParameter("query_media_folder");
194                         mergeData.put("query_media_folder",query_media_folder);
195                         String offset = req.getParameter("offset");
196                         if (offset==null || offset.equals("")) offset="0";
197                         mergeData.put("offset",offset);
198
199                         String order = req.getParameter("order");
200                         if (order==null) order="webdb_lastchange desc";
201
202                         // if in connection mode to content
203                         String cid = req.getParameter("cid");
204                         mergeData.put("cid",cid);
205
206
207                         // sql basteln
208                         String whereClause=""; boolean isFirst=true;
209                         if (query_text!=null && !query_text.equalsIgnoreCase("")) {
210                                 whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
211                         if (query_is_published != null && !query_is_published.equals("")) {
212                                 if (isFirst==false) whereClause+=" and ";
213                                 whereClause += "is_published='"+query_is_published+"'";
214                                 isFirst=false;
215                         }
216                         if (query_media_folder != null && !query_media_folder.equals("")) {
217                                 if (isFirst==false) whereClause+=" and ";
218                                 whereClause += "to_media_folder='"+query_media_folder+"'";
219                         }
220                         //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
221
222                         // fetch und ausliefern
223                         try {
224                                 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
225                                 mergeData.put("mediafolderHashdata", mediafolderModule.getHashData());
226                                 if (query_text!=null || query_is_published!=null || query_media_folder!=null) {
227                                         EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(),10);
228                                         if (theList != null) {
229                                                 mergeData.put("contentlist",HTMLTemplateProcessor.makeSimpleList(theList));
230                                                 if(theList.getOrder()!=null) {
231                                                         mergeData.put("order", theList.getOrder());
232                                                         mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
233                                                 }
234                                                 mergeData.put("count", (new Integer(theList.getCount())).toString());
235                                                 mergeData.put("from", (new Integer(theList.getFrom())).toString());
236                                                 mergeData.put("to", (new Integer(theList.getTo())).toString());
237                                                 if (theList.hasNextBatch())
238                                                         mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
239                                                 if (theList.hasPrevBatch())
240                                                         mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
241                                         }
242                                 }
243                                 // raus damit
244                                 HTMLTemplateProcessor.process(getLanguage(req)+"/"+templateListString, mergeData, res.getWriter());
245                         }
246                         catch (ModuleException e) {throw new ServletModuleException(e.toString());}
247                         catch (IOException e) {throw new ServletModuleException(e.toString());}
248                         catch (Exception e) {throw new ServletModuleException(e.toString());}
249         }
250
251
252         public void add(HttpServletRequest req, HttpServletResponse res)
253         throws ServletModuleException
254         {
255                 try {
256                         SimpleHash mergeData = new SimpleHash();
257                         mergeData.put("new", "1");
258                         mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
259                         deliver(req, res, mergeData, templateObjektString);
260                 }
261                 catch (Exception e) { throw new ServletModuleException(e.toString());}
262         }
263
264         public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
265         {
266                 String        idParam = req.getParameter("id");
267                 _edit(idParam, req, res);
268         }
269
270         private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
271                 if (idParam!=null && !idParam.equals("")) {
272                         try {
273                                 SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(mainModule.getById(idParam));
274                                 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
275                                 mergeData.put("rightsHashdata", dbRights.getHashData());
276                                 mergeData.put("imgformatHashdata", dbImageFormat.getHashData());
277                                 mergeData.put("imgcolorHashdata", dbImageColor.getHashData());
278                                 mergeData.put("imgtypeHashdata", dbImageType.getHashData());
279                                 mergeData.put("imglayoutHashdata", dbImageLayout.getHashData());
280                                 deliver(req, res, mergeData, templateObjektString);
281                         }
282                         catch (ModuleException e) { throw new ServletModuleException(e.toString());}
283                 }
284                 else throw new ServletModuleException("ServletmoduleImage :: _edit without id");
285         }
286
287
288         /** @todo should be in ServletModule.java */
289         private EntityUsers _getUser(HttpServletRequest req)
290         {
291                 HttpSession session=req.getSession(false);
292                 return (EntityUsers)session.getAttribute("login.uid");
293         }
294
295
296         // deprecated
297         public void upload(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
298         {
299                 //theLog.printDebugInfo("-- trying to upload");
300                 String idParam = req.getParameter("id");
301                 if (idParam!=null && !idParam.equals("")) {
302                         try {
303
304                                 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
305                                 HashMap withValues = mp.getParameters();
306                                 byte[] imageData=mp.getMedia();
307                                 String fileName=mp.getFilename();
308
309                                 int fileType = -1;
310                                 if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
311                                 if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
312                                 if (fileType>=0) {
313                                         EntityImage entImage = (EntityImage)mainModule.getById(idParam);
314                                         entImage.setImage(imageData, fileType);
315                                 }
316                                 else
317                                         theLog.printError("Wrong file uploaded!");
318                         }
319                         catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
320                         catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
321                 }
322                 else // keine id
323                         throw new ServletModuleException("Keine id angegeben");
324                 edit(req,res);
325         }
326
327
328 }