Converted media Interface to use streams (Java IO) instead of byte buffers of
[mir.git] / source / mircoders / servlet / ServletModuleUploadedMedia.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.servlet;
33
34 import freemarker.template.SimpleHash;
35 import freemarker.template.SimpleList;
36 import mir.entity.Entity;
37 import mir.entity.EntityList;
38 import mir.media.MediaHelper;
39 import mir.media.MirMedia;
40 import mir.media.MirMediaException;
41 import mir.media.MirMediaUserException;
42 import mir.misc.*;
43 import mir.module.ModuleException;
44 import mir.servlet.ServletModule;
45 import mir.servlet.ServletModuleException;
46 import mir.servlet.ServletModuleUserException;
47 import mir.storage.Database;
48 import mir.storage.StorageObjectException;
49 import mircoders.entity.EntityUsers;
50 import mircoders.storage.DatabaseMediaType;
51 import mircoders.storage.DatabaseMediafolder;
52 import mircoders.media.MediaRequest;
53
54 import javax.servlet.http.HttpServletRequest;
55 import javax.servlet.http.HttpServletResponse;
56 import javax.servlet.http.HttpSession;
57 import javax.servlet.ServletContext;
58
59 import java.io.IOException;
60 import java.net.URLEncoder;
61 import java.util.GregorianCalendar;
62 import java.util.HashMap;
63
64 /*
65  *  ServletModuleBilder -
66  *  liefert HTML fuer Bilder
67  *
68  *
69  * @author RK
70  */
71
72 public abstract class ServletModuleUploadedMedia
73         extends mir.servlet.ServletModule {
74
75   //private static DatabaseRights dbRights;
76
77   public static ServletModule getInstance() {
78     return null;
79   }
80
81   public void insert(HttpServletRequest req, HttpServletResponse res)
82           throws ServletModuleException, ServletModuleUserException {
83     try {
84       EntityUsers user = _getUser(req);
85       MediaRequest mediaReq =  new MediaRequest(user.getId(), false, false);
86       WebdbMultipartRequest mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);
87       EntityList mediaList = mediaReq.getEntityList();
88
89       SimpleHash mergeData = new SimpleHash();
90       SimpleHash popups = new SimpleHash();
91       mergeData.put("contentlist", mediaList);
92       if (mediaList.getOrder() != null) {
93         mergeData.put("order", mediaList.getOrder());
94         mergeData.put("order_encoded", URLEncoder.encode(mediaList.getOrder()));
95       }
96       mergeData.put("count", (new Integer(mediaList.getCount())).toString());
97       mergeData.put("from", (new Integer(mediaList.getFrom())).toString());
98       mergeData.put("to", (new Integer(mediaList.getTo())).toString());
99       if (mediaList.hasNextBatch())
100         mergeData.put("next", (new Integer(mediaList.getNextBatch())).toString());
101       if (mediaList.hasPrevBatch())
102           mergeData.put("prev", (new Integer(mediaList.getPrevBatch())).toString());
103       //fetch the popups
104       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
105       // raus damit
106       deliver(req, res, mergeData, popups, templateListString);
107     }
108     catch (FileHandlerUserException e) {
109       throw new ServletModuleUserException(e.getMsg());
110     }
111     catch (FileHandlerException e) {
112       throw new ServletModuleException(
113               "upload -- media handling exception " + e.toString());
114     }
115     catch (StorageObjectException e) {
116       throw new ServletModuleException("upload -- storageobjectexception "
117                                       + e.toString());
118     }
119     catch (IOException e) {
120       throw new ServletModuleException("upload -- ioexception " + e.toString());
121     }
122   }
123
124   public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
125
126     try {
127       EntityUsers user = _getUser(req);
128       WebdbMultipartRequest mp = new WebdbMultipartRequest(req, null);
129       HashMap parameters = mp.getParameters();
130
131       parameters.put("to_publisher", user.getId());
132       parameters.put("is_produced", "0");
133       if (!parameters.containsKey("is_published"))
134         parameters.put("is_published", "0");
135
136       String id = mainModule.set(parameters);
137       theLog.printError("media ID" + id);
138       _edit(id, req, res);
139     }
140     catch (IOException e) {
141       throw new ServletModuleException("upload -- ioexception " + e.toString());
142     }
143     catch (ModuleException e) {
144       throw new ServletModuleException("upload -- moduleexception " + e.toString());
145     }
146     catch (Exception e) {
147       throw new ServletModuleException("upload -- exception " + e.toString());
148     }
149
150   }
151
152
153   public void list(HttpServletRequest req, HttpServletResponse res)
154           throws ServletModuleException {
155     // Parameter auswerten
156     SimpleHash mergeData = new SimpleHash();
157     SimpleHash popups = new SimpleHash();
158
159     String query_text = req.getParameter("query_text");
160     mergeData.put("query_text", query_text);
161     if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
162     String query_field = req.getParameter("query_field");
163     mergeData.put("query_field", query_field);
164     String query_is_published = req.getParameter("query_is_published");
165     mergeData.put("query_is_published", query_is_published);
166     String query_media_folder = req.getParameter("query_media_folder");
167     mergeData.put("query_media_folder", query_media_folder);
168     String offset = req.getParameter("offset");
169     if (offset == null || offset.equals("")) offset = "0";
170     mergeData.put("offset", offset);
171
172     String order = req.getParameter("order");
173     if (order == null) order = "webdb_lastchange desc";
174
175     // if in connection mode to content
176     String cid = req.getParameter("cid");
177     mergeData.put("cid", cid);
178
179
180     // sql basteln
181     String whereClause = "";
182     boolean isFirst = true;
183     if (query_text != null && !query_text.equalsIgnoreCase("")) {
184       whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
185       isFirst = false;
186     }
187     if (query_is_published != null && !query_is_published.equals("")) {
188       if (isFirst == false) whereClause += " and ";
189       whereClause += "is_published='" + query_is_published + "'";
190       isFirst = false;
191     }
192     if (query_media_folder != null && !query_media_folder.equals("")) {
193       if (isFirst == false) whereClause += " and ";
194       whereClause += "to_media_folder='" + query_media_folder + "'";
195     }
196     //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
197
198     // fetch und ausliefern
199     try {
200       if (query_text != null || query_is_published != null || query_media_folder != null) {
201         EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
202         if (theList != null) {
203           mergeData.put("contentlist", theList);
204           if (theList.getOrder() != null) {
205             mergeData.put("order", theList.getOrder());
206             mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
207           }
208           mergeData.put("count", (new Integer(theList.getCount())).toString());
209           mergeData.put("from", (new Integer(theList.getFrom())).toString());
210           mergeData.put("to", (new Integer(theList.getTo())).toString());
211           if (theList.hasNextBatch())
212             mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
213           if (theList.hasPrevBatch())
214             mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
215         }
216       }
217       //fetch the popups
218       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
219       // raus damit
220       deliver(req, res, mergeData, popups, templateListString);
221     }
222     catch (ModuleException e) {
223       throw new ServletModuleException(e.toString());
224     }
225     catch (Exception e) {
226       throw new ServletModuleException(e.toString());
227     }
228   }
229
230
231   public void add(HttpServletRequest req, HttpServletResponse res)
232           throws ServletModuleException {
233     try {
234       SimpleHash mergeData = new SimpleHash();
235       mergeData.put("new", "1");
236       SimpleHash popups = new SimpleHash();
237       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
238       String maxMedia = MirConfig.getProp("ServletModule.OpenIndy.MaxMediaUploadItems");
239       String numOfMedia = req.getParameter("medianum");
240       if(numOfMedia==null||numOfMedia.equals("")){
241         numOfMedia="1";
242       } else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
243         numOfMedia = maxMedia;
244       }
245     
246       int mediaNum = Integer.parseInt(numOfMedia);
247       SimpleList mediaFields = new SimpleList();
248       for(int i =0; i<mediaNum;i++){
249         Integer mNum = new Integer(i+1);
250         mediaFields.add(mNum.toString());
251       }
252       mergeData.put("medianum",numOfMedia);
253       mergeData.put("mediafields",mediaFields);
254       deliver(req, res, mergeData, popups, templateObjektString);
255     } catch (Exception e) {
256       throw new ServletModuleException(e.toString());
257     }
258   }
259
260   public void edit(HttpServletRequest req, HttpServletResponse res)
261           throws ServletModuleException {
262     String idParam = req.getParameter("id");
263     _edit(idParam, req, res);
264   }
265
266   private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res)
267           throws ServletModuleException {
268     if (idParam != null && !idParam.equals("")) {
269       try {
270         SimpleHash popups = new SimpleHash();
271         popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
272         deliver(req, res, mainModule.getById(idParam), popups,
273                 templateObjektString);
274       }
275       catch (ModuleException e) {
276         throw new ServletModuleException(e.toString());
277       }
278       catch (StorageObjectException e) {
279         throw new ServletModuleException(e.toString());
280       }
281     }
282     else {
283       throw new ServletModuleException("ServletmoduleUploadedMedia :: _edit without id");
284     }
285   }
286
287
288   /** @todo should be in ServletModule.java */
289   private EntityUsers _getUser(HttpServletRequest req) {
290     HttpSession session = req.getSession(false);
291     return (EntityUsers) session.getAttribute("login.uid");
292   }
293
294 }
295
296