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