Mir goes GPL
[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.MirConfig;
43 import mir.misc.MpRequest;
44 import mir.misc.StringUtil;
45 import mir.misc.WebdbMultipartRequest;
46 import mir.module.ModuleException;
47 import mir.servlet.ServletModule;
48 import mir.servlet.ServletModuleException;
49 import mir.servlet.ServletModuleUserException;
50 import mir.storage.Database;
51 import mir.storage.StorageObjectException;
52 import mircoders.entity.EntityUsers;
53 import mircoders.storage.DatabaseMediaType;
54 import mircoders.storage.DatabaseMediafolder;
55 import mircoders.media.MediaRequest;
56
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59 import javax.servlet.http.HttpSession;
60 import javax.servlet.ServletContext;
61
62 import java.io.IOException;
63 import java.net.URLEncoder;
64 import java.util.GregorianCalendar;
65 import java.util.HashMap;
66
67 /*
68  *  ServletModuleBilder -
69  *  liefert HTML fuer Bilder
70  *
71  *
72  * @author RK
73  */
74
75 public abstract class ServletModuleUploadedMedia
76         extends mir.servlet.ServletModule {
77
78   //private static DatabaseRights dbRights;
79
80   public static ServletModule getInstance() {
81     return null;
82   }
83
84   public void insert(HttpServletRequest req, HttpServletResponse res)
85           throws ServletModuleException, ServletModuleUserException {
86     try {
87       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
88       EntityUsers user = _getUser(req);
89       EntityList mediaList =
90         new MediaRequest(mp, user.getId()).getMedia(false, false);
91       list(req, res);
92     }
93     catch (MirMediaUserException e) {
94       throw new ServletModuleUserException(e.getMsg());
95     }
96     catch (MirMediaException e) {
97       throw new ServletModuleException(
98               "upload -- media handling exception " + e.toString());
99     }
100     catch (IOException e) {
101       throw new ServletModuleException("upload -- ioexception " + e.toString());
102     }
103   }
104
105   public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
106
107     try {
108       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
109       HashMap parameters = mp.getParameters();
110
111       EntityUsers user = _getUser(req);
112       parameters.put("to_publisher", user.getId());
113       parameters.put("is_produced", "0");
114       if (!parameters.containsKey("is_published"))
115         parameters.put("is_published", "0");
116
117       String id = mainModule.set(parameters);
118       theLog.printError("media ID" + id);
119       _edit(id, req, res);
120     }
121     catch (IOException e) {
122       throw new ServletModuleException("upload -- ioexception " + e.toString());
123     }
124     catch (ModuleException e) {
125       throw new ServletModuleException("upload -- moduleexception " + e.toString());
126     }
127
128   }
129
130
131   public void list(HttpServletRequest req, HttpServletResponse res)
132           throws ServletModuleException {
133     // Parameter auswerten
134     SimpleHash mergeData = new SimpleHash();
135     SimpleHash popups = new SimpleHash();
136
137     String query_text = req.getParameter("query_text");
138     mergeData.put("query_text", query_text);
139     if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
140     String query_field = req.getParameter("query_field");
141     mergeData.put("query_field", query_field);
142     String query_is_published = req.getParameter("query_is_published");
143     mergeData.put("query_is_published", query_is_published);
144     String query_media_folder = req.getParameter("query_media_folder");
145     mergeData.put("query_media_folder", query_media_folder);
146     String offset = req.getParameter("offset");
147     if (offset == null || offset.equals("")) offset = "0";
148     mergeData.put("offset", offset);
149
150     String order = req.getParameter("order");
151     if (order == null) order = "webdb_lastchange desc";
152
153     // if in connection mode to content
154     String cid = req.getParameter("cid");
155     mergeData.put("cid", cid);
156
157
158     // sql basteln
159     String whereClause = "";
160     boolean isFirst = true;
161     if (query_text != null && !query_text.equalsIgnoreCase("")) {
162       whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
163       isFirst = false;
164     }
165     if (query_is_published != null && !query_is_published.equals("")) {
166       if (isFirst == false) whereClause += " and ";
167       whereClause += "is_published='" + query_is_published + "'";
168       isFirst = false;
169     }
170     if (query_media_folder != null && !query_media_folder.equals("")) {
171       if (isFirst == false) whereClause += " and ";
172       whereClause += "to_media_folder='" + query_media_folder + "'";
173     }
174     //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
175
176     // fetch und ausliefern
177     try {
178       if (query_text != null || query_is_published != null || query_media_folder != null) {
179         EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
180         if (theList != null) {
181           mergeData.put("contentlist", theList);
182           if (theList.getOrder() != null) {
183             mergeData.put("order", theList.getOrder());
184             mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
185           }
186           mergeData.put("count", (new Integer(theList.getCount())).toString());
187           mergeData.put("from", (new Integer(theList.getFrom())).toString());
188           mergeData.put("to", (new Integer(theList.getTo())).toString());
189           if (theList.hasNextBatch())
190             mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
191           if (theList.hasPrevBatch())
192             mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
193         }
194       }
195       //fetch the popups
196       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
197       // raus damit
198       deliver(req, res, mergeData, popups, templateListString);
199     }
200     catch (ModuleException e) {
201       throw new ServletModuleException(e.toString());
202     }
203     catch (Exception e) {
204       throw new ServletModuleException(e.toString());
205     }
206   }
207
208
209   public void add(HttpServletRequest req, HttpServletResponse res)
210           throws ServletModuleException {
211     try {
212       SimpleHash mergeData = new SimpleHash();
213       mergeData.put("new", "1");
214       SimpleHash popups = new SimpleHash();
215       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
216       String maxMedia = MirConfig.getProp("ServletModule.OpenIndy.MaxMediaUploadItems");
217       String numOfMedia = req.getParameter("medianum");
218       if(numOfMedia==null||numOfMedia.equals("")){
219         numOfMedia="1";
220       } else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
221         numOfMedia = maxMedia;
222       }
223     
224       int mediaNum = Integer.parseInt(numOfMedia);
225       SimpleList mediaFields = new SimpleList();
226       for(int i =0; i<mediaNum;i++){
227         Integer mNum = new Integer(i+1);
228         mediaFields.add(mNum.toString());
229       }
230       mergeData.put("medianum",numOfMedia);
231       mergeData.put("mediafields",mediaFields);
232       deliver(req, res, mergeData, popups, templateObjektString);
233     } catch (Exception e) {
234       throw new ServletModuleException(e.toString());
235     }
236   }
237
238   public void edit(HttpServletRequest req, HttpServletResponse res)
239           throws ServletModuleException {
240     String idParam = req.getParameter("id");
241     _edit(idParam, req, res);
242   }
243
244   private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res)
245           throws ServletModuleException {
246     if (idParam != null && !idParam.equals("")) {
247       try {
248         SimpleHash popups = new SimpleHash();
249         popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
250         deliver(req, res, mainModule.getById(idParam), popups,
251                 templateObjektString);
252       }
253       catch (ModuleException e) {
254         throw new ServletModuleException(e.toString());
255       }
256       catch (StorageObjectException e) {
257         throw new ServletModuleException(e.toString());
258       }
259     }
260     else {
261       throw new ServletModuleException("ServletmoduleUploadedMedia :: _edit without id");
262     }
263   }
264
265
266   /** @todo should be in ServletModule.java */
267   private EntityUsers _getUser(HttpServletRequest req) {
268     HttpSession session = req.getSession(false);
269     return (EntityUsers) session.getAttribute("login.uid");
270   }
271
272 }
273
274