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