1 package mircoders.servlet;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
10 import freemarker.template.*;
11 import com.oreilly.servlet.multipart.*;
12 import com.oreilly.servlet.*;
20 import mircoders.entity.*;
21 import mircoders.storage.*;
22 import mircoders.module.*;
25 * ServletModuleBilder -
26 * liefert HTML fuer Bilder
32 public class ServletModuleImages extends mir.servlet.ServletModule
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;
42 // Singelton / Kontruktor
43 private static ServletModuleImages instance = new ServletModuleImages();
44 public static ServletModule getInstance() { return instance; }
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");
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();
61 catch (StorageObjectException e) {
62 theLog.printDebugInfo("servletmodulebilder konnte nicht initialisiert werden");
67 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
70 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
71 HashMap parameters = mp.getParameters();
72 byte[] imageData=mp.getMedia();
73 String fileName=mp.getFilename();
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");
82 String id = mainModule.add(parameters);
83 EntityImage entImage = (EntityImage)mainModule.getById(id);
85 if (imageData!=null && fileName!=null) {
87 if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
88 if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
90 entImage.setImage(imageData, fileType);
92 theLog.printError("Wrong file uploaded!");
96 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
97 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
101 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
105 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
106 HashMap parameters = mp.getParameters();
107 byte[] imageData=mp.getMedia();
108 String fileName=mp.getFilename();
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");
116 String id = mainModule.set(parameters);
117 EntityImage entImage = (EntityImage)mainModule.getById(id);
119 if (imageData!=null && fileName!=null) {
121 if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
122 if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
124 entImage.setImage(imageData, fileType);
126 theLog.printError("Wrong file uploaded!");
130 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
131 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
136 public void showimg(HttpServletRequest req, HttpServletResponse res)
137 throws ServletModuleException
139 String idParam = req.getParameter("id");
140 if (idParam!=null && !idParam.equals("")) {
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
146 byte[] outbytes = entImage.getImage();
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());}
155 else theLog.printDebugInfo("id nicht angeben.");
156 // darf keine exception werfen
159 public void showicon(HttpServletRequest req, HttpServletResponse res)
160 throws ServletModuleException
162 String idParam = req.getParameter("id");
163 if (idParam!=null && !idParam.equals("")) {
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
169 byte[] outbytes = entImage.getIcon();
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());}
178 else throw new ServletModuleException("id nicht angeben.");
181 public void list(HttpServletRequest req, HttpServletResponse res)
182 throws ServletModuleException
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);
199 String order = req.getParameter("order");
200 if (order==null) order="webdb_lastchange desc";
202 // if in connection mode to content
203 String cid = req.getParameter("cid");
204 mergeData.put("cid",cid);
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+"'";
216 if (query_media_folder != null && !query_media_folder.equals("")) {
217 if (isFirst==false) whereClause+=" and ";
218 whereClause += "to_media_folder='"+query_media_folder+"'";
220 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
222 // fetch und ausliefern
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()));
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());
244 HTMLTemplateProcessor.process(getLanguage(req)+"/"+templateListString, mergeData, res.getWriter());
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());}
252 public void add(HttpServletRequest req, HttpServletResponse res)
253 throws ServletModuleException
256 SimpleHash mergeData = new SimpleHash();
257 mergeData.put("new", "1");
258 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
259 deliver(req, res, mergeData, templateObjektString);
261 catch (Exception e) { throw new ServletModuleException(e.toString());}
264 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
266 String idParam = req.getParameter("id");
267 _edit(idParam, req, res);
270 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
271 if (idParam!=null && !idParam.equals("")) {
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);
282 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
284 else throw new ServletModuleException("ServletmoduleImage :: _edit without id");
288 /** @todo should be in ServletModule.java */
289 private EntityUsers _getUser(HttpServletRequest req)
291 HttpSession session=req.getSession(false);
292 return (EntityUsers)session.getAttribute("login.uid");
297 public void upload(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
299 //theLog.printDebugInfo("-- trying to upload");
300 String idParam = req.getParameter("id");
301 if (idParam!=null && !idParam.equals("")) {
304 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
305 HashMap withValues = mp.getParameters();
306 byte[] imageData=mp.getMedia();
307 String fileName=mp.getFilename();
310 if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
311 if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
313 EntityImage entImage = (EntityImage)mainModule.getById(idParam);
314 entImage.setImage(imageData, fileType);
317 theLog.printError("Wrong file uploaded!");
319 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
320 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
323 throw new ServletModuleException("Keine id angegeben");