1 package mircoders.servlet;
8 import java.lang.reflect.*;
9 import javax.servlet.*;
10 import javax.servlet.http.*;
12 import freemarker.template.*;
13 import com.oreilly.servlet.multipart.*;
14 import com.oreilly.servlet.*;
23 import mircoders.entity.*;
24 import mircoders.storage.*;
25 import mircoders.module.*;
26 import mircoders.producer.*;
29 * ServletModuleBilder -
30 * liefert HTML fuer Bilder
36 public class ServletModuleImages extends mir.servlet.ServletModule
39 //private static DatabaseRights dbRights;
41 // Singelton / Kontruktor
42 private static ServletModuleImages instance = new ServletModuleImages();
43 public static ServletModule getInstance() { return instance; }
46 private ServletModuleImages() {
47 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Bilder.Logfile"));
48 templateListString = MirConfig.getProp("ServletModule.Bilder.ListTemplate");
49 templateObjektString = MirConfig.getProp("ServletModule.Bilder.ObjektTemplate");
50 templateConfirmString = MirConfig.getProp("ServletModule.Bilder.ConfirmTemplate");
52 mainModule = new ModuleImages(DatabaseImages.getInstance());
53 //dbRights = DatabaseRights.getInstance();
55 catch (StorageObjectException e) {
56 theLog.printDebugInfo("servletmodulebilder konnte nicht initialisiert werden");
61 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
64 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
65 HashMap parameters = mp.getParameters();
67 MpRequest mpReq = (MpRequest)mp.requestList.get(0);
68 byte[] imageData=mpReq.getMedia();
69 String fileName=mpReq.getFilename();
70 String contentType= mpReq.getContentType();
72 EntityUsers user = _getUser(req);
73 parameters.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
74 parameters.put("to_publisher", user.getId());
75 parameters.put("is_produced", "0");
76 if (!parameters.containsKey("is_published"))
77 parameters.put("is_published","0");
79 String id = mainModule.add(parameters);
80 EntityImages entImage = (EntityImages)mainModule.getById(id);
82 if (imageData!=null && fileName!=null) {
83 //the where clause to find the media_type entry
84 //from the content-type.
85 //we use the media type entry to lookup the
86 //media Handler/Storage classes
87 String wc = " mime_type='"+contentType+"'";
89 EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
90 String mediaTypeId = null;
91 String mediaStorageName = null;
92 String mediaHandlerName = null;
94 //if we found an entry matching the
95 //content-type int the table.
96 if (mediaTypesList.size() > 0) {
97 //get the class names from the media_type table.
98 mediaTypeId = mediaTypesList.elementAt(0).getId();
99 mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
100 mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
101 parameters.put("to_media_type",mediaTypeId);
103 //load the classes via reflection
106 Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
107 Method m = mediaStorageClass.getMethod("getInstance", null);
108 Database mediaStorage = (Database)m.invoke(null, null);
109 Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
110 mediaEnt.setStorage(mediaStorage);
111 mediaEnt.setValues(parameters);
112 mediaId = mediaEnt.insert();
114 Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
115 MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
116 //save and store the media data/metadata
117 mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
119 //were done with mpReq at this point, dereference it. as it contains
120 //mucho mem. -mh 01.10.2001
124 new ProducerImages().handle(null, null, false, false, mediaId);
126 } catch (Exception e) {
127 theLog.printError("setting uploaded_media failed: "+e.toString());
131 entImage.setImage(imageData);
136 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
137 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
138 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
142 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
146 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
147 HashMap parameters = mp.getParameters();
148 //MpRequest mpReq = (MpRequest)mp.requestList.get(0);
149 //byte[] imageData=mpReq.getMedia();
150 //String fileName=mpReq.getFilename();
151 //String contentType=mpReq.getContentType();
153 EntityUsers user = _getUser(req);
154 parameters.put("to_publisher", user.getId());
155 parameters.put("is_produced", "0");
156 if (!parameters.containsKey("is_published"))
157 parameters.put("is_published","0");
159 String id = mainModule.set(parameters);
160 theLog.printError("Image ID"+id);
163 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
164 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
169 public void showimg(HttpServletRequest req, HttpServletResponse res)
170 throws ServletModuleException
172 String idParam = req.getParameter("id");
173 if (idParam!=null && !idParam.equals("")) {
175 EntityImages entImage =(EntityImages)mainModule.getById(idParam);
176 res.setContentType("image/jpeg"); // testweise
177 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
179 byte[] outbytes = entImage.getImage();
184 catch (IOException e) {throw new ServletModuleException(e.toString());}
185 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
186 catch (Exception e) {throw new ServletModuleException(e.toString());}
188 else theLog.printDebugInfo("id nicht angeben.");
189 // darf keine exception werfen
192 public void showicon(HttpServletRequest req, HttpServletResponse res)
193 throws ServletModuleException
195 String idParam = req.getParameter("id");
196 if (idParam!=null && !idParam.equals("")) {
198 EntityImages entImage =(EntityImages)mainModule.getById(idParam);
199 res.setContentType("image/jpeg"); // testweise
200 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
202 byte[] outbytes = entImage.getIcon();
207 catch (IOException e) {throw new ServletModuleException(e.toString());}
208 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
209 catch (Exception e) {throw new ServletModuleException(e.toString());}
211 else throw new ServletModuleException("id nicht angeben.");
214 public void list(HttpServletRequest req, HttpServletResponse res)
215 throws ServletModuleException
217 // Parameter auswerten
218 SimpleHash mergeData = new SimpleHash();
219 String query_text = req.getParameter("query_text");
220 mergeData.put("query_text",query_text);
221 if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
222 String query_field = req.getParameter("query_field");
223 mergeData.put("query_field",query_field);
224 String query_is_published = req.getParameter("query_is_published");
225 mergeData.put("query_is_published",query_is_published);
226 String query_media_folder = req.getParameter("query_media_folder");
227 mergeData.put("query_media_folder",query_media_folder);
228 String offset = req.getParameter("offset");
229 if (offset==null || offset.equals("")) offset="0";
230 mergeData.put("offset",offset);
232 String order = req.getParameter("order");
233 if (order==null) order="webdb_lastchange desc";
235 // if in connection mode to content
236 String cid = req.getParameter("cid");
237 mergeData.put("cid",cid);
241 String whereClause=""; boolean isFirst=true;
242 if (query_text!=null && !query_text.equalsIgnoreCase("")) {
243 whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
244 if (query_is_published != null && !query_is_published.equals("")) {
245 if (isFirst==false) whereClause+=" and ";
246 whereClause += "is_published='"+query_is_published+"'";
249 if (query_media_folder != null && !query_media_folder.equals("")) {
250 if (isFirst==false) whereClause+=" and ";
251 whereClause += "to_media_folder='"+query_media_folder+"'";
253 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
255 // fetch und ausliefern
257 if (query_text!=null || query_is_published!=null || query_media_folder!=null) {
258 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(),10);
259 if (theList != null) {
260 mergeData.put("contentlist",theList);
261 if(theList.getOrder()!=null) {
262 mergeData.put("order", theList.getOrder());
263 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
265 mergeData.put("count", (new Integer(theList.getCount())).toString());
266 mergeData.put("from", (new Integer(theList.getFrom())).toString());
267 mergeData.put("to", (new Integer(theList.getTo())).toString());
268 if (theList.hasNextBatch())
269 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
270 if (theList.hasPrevBatch())
271 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
275 HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString,
276 mergeData, res.getWriter(),req.getLocale());
278 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
279 catch (IOException e) {throw new ServletModuleException(e.toString());}
280 catch (Exception e) {throw new ServletModuleException(e.toString());}
284 public void add(HttpServletRequest req, HttpServletResponse res)
285 throws ServletModuleException
288 SimpleHash mergeData = new SimpleHash();
289 mergeData.put("new", "1");
290 deliver(req, res, mergeData, templateObjektString);
292 catch (Exception e) { throw new ServletModuleException(e.toString());}
295 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
297 String idParam = req.getParameter("id");
298 _edit(idParam, req, res);
301 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
302 if (idParam!=null && !idParam.equals("")) {
304 //mergeData.put("rightsHashdata", dbRights.getHashData());
305 deliver(req, res, mainModule.getById(idParam), templateObjektString);
307 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
309 else throw new ServletModuleException("ServletmoduleImage :: _edit without id");
313 /** @todo should be in ServletModule.java */
314 private EntityUsers _getUser(HttpServletRequest req)
316 HttpSession session=req.getSession(false);
317 return (EntityUsers)session.getAttribute("login.uid");
322 public void upload(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
324 //theLog.printDebugInfo("-- trying to upload");
325 String idParam = req.getParameter("id");
326 if (idParam!=null && !idParam.equals("")) {
329 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
330 HashMap withValues = mp.getParameters();
331 MpRequest mpReq = (MpRequest)mp.requestList.get(0);
332 byte[] imageData=mpReq.getMedia();
333 String fileName=mpReq.getFilename();
334 String contentType=mpReq.getContentType();
336 EntityImages entImage = (EntityImages)mainModule.getById(idParam);
337 entImage.setImage(imageData);
339 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
340 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
341 catch (StorageObjectException e) { throw new ServletModuleException("upload -- storageobjectexception " + e.toString());}
344 throw new ServletModuleException("Keine id angegeben");