51527179f62ba5385a0233de1d7f6871b8058caf
[mir.git] / source / mircoders / servlet / ServletModuleLinksImcs.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
36 import java.io.IOException;
37 import java.io.PrintWriter;
38 import java.net.URLEncoder;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43 import mir.entity.EntityList;
44 import mir.misc.HTMLParseException;
45 import mir.misc.HTMLTemplateProcessor;
46 import mir.misc.Logfile;
47 import mir.misc.MirConfig;
48 import mir.module.ModuleException;
49 import mir.servlet.ServletModule;
50 import mir.servlet.ServletModuleException;
51 import mir.storage.StorageObjectException;
52 import mir.log.*;
53
54 import mircoders.module.ModuleLanguage;
55 import mircoders.module.ModuleLinksImcs;
56 import mircoders.storage.DatabaseLanguage;
57 import mircoders.storage.DatabaseLinksImcs;
58
59 /*
60  *  ServletModuleLinksImcs -
61  *  liefert HTML fuer LinksImcs
62  *
63  *
64  * @author RK
65  */
66
67 public class ServletModuleLinksImcs extends ServletModule {
68   private ModuleLanguage languageModule;
69
70   // Singelton / Kontruktor
71   private static ServletModuleLinksImcs instance = new ServletModuleLinksImcs();
72
73   public static ServletModule getInstance() {
74     return instance;
75   }
76
77
78   private ServletModuleLinksImcs() {
79     logger = new LoggerWrapper("ServletModule.LinksImcs");
80     templateListString = MirConfig.getProp("ServletModule.LinksImcs.ListTemplate");
81     templateObjektString = MirConfig.getProp("ServletModule.LinksImcs.ObjektTemplate");
82     templateConfirmString = MirConfig.getProp("ServletModule.LinksImcs.ConfirmTemplate");
83
84     try {
85       mainModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance());
86       languageModule = new ModuleLanguage(DatabaseLanguage.getInstance());
87     }
88     catch (StorageObjectException e) {
89       logger.error("Initialization of ServletModuleLinksImcs failed!: " + e.getMessage());
90     }
91   }
92
93   public void add(HttpServletRequest req, HttpServletResponse res)
94           throws ServletModuleException {
95     try {
96       SimpleHash modelRoot = new SimpleHash();
97       EntityList theParentList;
98       EntityList theLanguageList;
99       int offset = 0;
100
101       theParentList = mainModule.getByWhereClause("to_parent_id=NULL", "title", offset, 1000);
102       theLanguageList = languageModule.getByWhereClause(null, "name", 0);
103
104       modelRoot.put("new", "1");
105       modelRoot.put("parentlist", theParentList);
106       modelRoot.put("languagelist", theLanguageList);
107
108       if (theParentList == null || theParentList.getCount() == 0 || theParentList.getCount() > 1) {
109         HTMLTemplateProcessor.process(res, templateObjektString, modelRoot, res.getWriter(), getLocale(req));
110       }
111       else {
112         deliver(req, res, modelRoot, templateObjektString);
113       }
114
115     }
116     catch (ModuleException e) {
117       throw new ServletModuleException(e.toString());
118     }
119     catch (HTMLParseException e) {
120       throw new ServletModuleException(e.toString());
121     }
122     catch (IOException e) {
123       throw new ServletModuleException(e.toString());
124     }
125   }
126
127   public void list(HttpServletRequest req, HttpServletResponse res)
128           throws ServletModuleException {
129     try {
130
131       SimpleHash modelRoot = new SimpleHash();
132       EntityList theParentList;
133       EntityList theImcsList;
134       EntityList theLanguageList;
135       String offsetParam = req.getParameter("offset");
136       String where = "";
137       String offset = "";
138       PrintWriter out = res.getWriter();
139
140       // Parameter auswerten
141       String query_text = req.getParameter("query_text");
142       modelRoot.put("query_text", query_text);
143       if (query_text != null) modelRoot.put("query_text_encoded", URLEncoder.encode(query_text));
144       String query_field = req.getParameter("query_field");
145       modelRoot.put("query_field", query_field);
146       String parent = req.getParameter("to_parent_id");
147       modelRoot.put("to_parent_id", parent);
148       String language = req.getParameter("to_language");
149       modelRoot.put("to_language", language);
150       modelRoot.put("language", getLanguage(req));
151
152       String whereClause = "";
153       boolean isFirst = true;
154       if (query_text != null && !query_text.equalsIgnoreCase("")) {
155         whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
156         isFirst = false;
157       }
158       if (parent != null && !parent.equals("")) {
159         if (isFirst == false) whereClause += " and ";
160         whereClause += "to_parent_id='" + parent + "'";
161         isFirst = false;
162       }
163       if (language != null && !language.equals("")) {
164         if (isFirst == false) whereClause += " and ";
165         whereClause += "to_language='" + language + "'";
166         isFirst = false;
167       }
168
169       // hier offsetcode bearbeiten
170       if (offsetParam != null && !offsetParam.equals("")) {
171         offset = offsetParam;
172       }
173       if (req.getParameter("next") != null) {
174         offset = req.getParameter("nextoffset");
175       }
176       else {
177         if (req.getParameter("prev") != null) {
178           offset = req.getParameter("prevoffset");
179         }
180       }
181
182       if (offset == null || offset.equals("")) offset = "0";
183       modelRoot.put("offset", (new Integer(offset)).toString());
184
185       theParentList = mainModule.getByWhereClause("to_parent_id=NULL", "title", 0, 1000);
186       theImcsList = mainModule.getByWhereClause(whereClause, "title", (new Integer(offset)).intValue());
187       theLanguageList = languageModule.getByWhereClause(null, "name", 0);
188
189       modelRoot.put("parentlist", theParentList);
190       modelRoot.put("imcslist", theImcsList);
191       modelRoot.put("languagelist", theLanguageList);
192       modelRoot.put("count", (new Integer(theImcsList.getCount())).toString());
193       modelRoot.put("from", (new Integer(theImcsList.getFrom())).toString());
194       modelRoot.put("to", (new Integer(theImcsList.getTo())).toString());
195       if (theImcsList.hasNextBatch())
196         modelRoot.put("next", (new Integer(theImcsList.getNextBatch())).toString());
197       if (theImcsList.hasPrevBatch())
198         modelRoot.put("prev", (new Integer(theImcsList.getPrevBatch())).toString());
199
200       HTMLTemplateProcessor.process(res, templateListString, modelRoot, res.getWriter(), getLocale(req));
201
202     }
203     catch (Exception e) {
204       throw new ServletModuleException(e.toString());
205     }
206   }
207
208   public void edit(HttpServletRequest req, HttpServletResponse res)
209           throws ServletModuleException {
210     try {
211
212       SimpleHash modelRoot = new SimpleHash();
213       EntityList parentList;
214       EntityList theLanguageList;
215       int offset = 0;
216       String idParam = req.getParameter("id");
217       String where = "";
218
219       parentList = mainModule.getByWhereClause("to_parent_id=NULL", "title", offset, 1000);
220       theLanguageList = languageModule.getByWhereClause(null, "name", 0);
221
222       modelRoot.put("parentlist", parentList);
223       modelRoot.put("languagelist", theLanguageList);
224       modelRoot.put("entity", mainModule.getById(idParam));
225       modelRoot.put("new", "0");
226       deliver(req, res, modelRoot, templateObjektString);
227
228     }
229     catch (ModuleException e) {
230       throw new ServletModuleException(e.toString());
231     }
232   }
233
234 }
235