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