001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.menu;
024:
025: import java.sql.SQLException;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.HashMap;
029: import java.util.HashSet;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035:
036: import biz.hammurapi.config.ConfigurationException;
037: import biz.hammurapi.sql.SQLProcessor;
038: import biz.hammurapi.web.ActionServlet;
039: import biz.hammurapi.web.ActionsBase;
040: import biz.hammurapi.web.HammurapiWebException;
041: import biz.hammurapi.web.RequestParametersContext;
042: import biz.hammurapi.web.SimpleRedirect;
043: import biz.hammurapi.web.eval.EvaluatorFactory;
044: import biz.hammurapi.web.menu.sql.MenuEngine;
045: import biz.hammurapi.web.menu.sql.Xmenu;
046: import biz.hammurapi.web.menu.sql.XmenuImpl;
047: import biz.hammurapi.web.security.sql.SecurityEngine;
048: import biz.hammurapi.web.security.sql.permission.PermissionEngine;
049: import biz.hammurapi.web.util.GuidGenerator;
050:
051: /**
052: * Web actions class for Menu and related objects.
053: * @author Pavel Vlasov
054: * @version $Revision$
055: */
056: public class MenuActions extends ActionsBase {
057:
058: private static final String VIEW_PATH = "get/view.html?id=";
059:
060: /**
061: * Lists root menu items.
062: * @param request
063: * @param response
064: * @param servlet
065: * @return
066: * @throws SQLException
067: */
068: public Collection listRoots(HttpServletRequest request,
069: HttpServletResponse response, ActionServlet servlet)
070: throws SQLException {
071: return getEngine(request).getRootMenus(new ArrayList());
072: }
073:
074: /**
075: * Returns user root menu.
076: * @param request
077: * @param response
078: * @param servlet
079: * @return
080: * @throws SQLException
081: */
082: public Object getRoot(HttpServletRequest request,
083: HttpServletResponse response, ActionServlet servlet)
084: throws SQLException {
085: return request.getSession().getAttribute(
086: MenuFilter.MENU_ATTRIBUTE);
087: }
088:
089: public static MenuEngine getEngine(HttpServletRequest request) {
090: return (MenuEngine) getGlobal(request, "db/MenuEngine");
091: }
092:
093: /**
094: * Retrieves menu information by 'id' parameter
095: * @param request
096: * @param response
097: * @param servlet
098: * @return
099: * @throws SQLException
100: * @throws ConfigurationException
101: */
102: public Object get(HttpServletRequest request,
103: HttpServletResponse response, ActionServlet servlet)
104: throws SQLException, ConfigurationException {
105: if ("yes".equals(request.getParameter("new"))) {
106: XmenuImpl ret = new XmenuImpl();
107: ret.configure(new RequestParametersContext(request),
108: converter); //
109: ret.setAttribute("new", "yes");
110: setReferrer(request, ret);
111: EvaluatorFactory eFactory = (EvaluatorFactory) getGlobal(
112: request, "db/EvaluatorFactory");
113: if (eFactory != null) {
114: ret
115: .setAttribute("evaluator-names", eFactory
116: .getNames());
117: }
118: return ret;
119: }
120:
121: String id = request.getParameter("id");
122: if (id == null) {
123: return "Menu id parameter is missing";
124: }
125:
126: MenuEngine engine = getEngine(request);
127: XmenuImpl ret = (XmenuImpl) engine.getXmenu(Integer
128: .parseInt(id));
129: if (ret == null) {
130: return "Invalid menu id: " + id;
131: }
132:
133: ret.setAttribute("children", engine.getXmenuByParent(
134: new Integer(ret.getId()), new ArrayList()));
135: ret.setAttribute("adornments", engine.getAdornmentByItem(ret
136: .getId(), new ArrayList()));
137: ret
138: .setAttribute("resourcePatterns", engine
139: .getResourcePatternByItem(ret.getId(),
140: new ArrayList()));
141: ret.setAttribute("colors", engine.getMenuColorByMenu(ret
142: .getId(), new ArrayList()));
143: ret.setAttribute("permissions", engine.getPermission(ret
144: .getId(), new ArrayList()));
145: ret.setAttribute("parameters", engine.getMenuParameterByMenu(
146: ret.getId(), new ArrayList()));
147: ret.setAttribute("functions", engine.getMenuFunctionByMenu(ret
148: .getId(), new ArrayList()));
149: ret.setAttribute("help-topics", engine.getMenuHelpTopics(ret
150: .getId(), new ArrayList()));
151: ret.setAttribute("navigator-templates", engine
152: .getMenuNavigatorTemplateByMenu(ret.getId(),
153: new ArrayList()));
154:
155: EvaluatorFactory eFactory = (EvaluatorFactory) getGlobal(
156: request, "db/EvaluatorFactory");
157: if (eFactory != null) {
158: ret.setAttribute("evaluator-names", eFactory.getNames());
159: }
160:
161: setReferrer(request, ret);
162: return ret;
163: }
164:
165: /**
166: * Updates menu.
167: * @param request
168: * @param response
169: * @param servlet
170: * @return
171: * @throws SQLException
172: * @throws ConfigurationException
173: * @throws HammurapiWebException
174: */
175: public Object update(HttpServletRequest request,
176: HttpServletResponse response, ActionServlet servlet)
177: throws SQLException, ConfigurationException,
178: HammurapiWebException {
179: boolean isUpdate = !"yes".equals(request.getParameter("_new"));
180:
181: XmenuImpl obj = new XmenuImpl(isUpdate);
182: obj.configure(new RequestParametersContext(request), converter);
183: MenuEngine engine = getEngine(request);
184:
185: // TODO - Add server side validations as column attributes.
186:
187: // if (hadErrors) {
188: // return obj;
189: // }
190:
191: GuidGenerator guidGenerator = (GuidGenerator) getGlobal(
192: request, "db/guid-generator");
193: if (guidGenerator != null && isBlank(obj.getGuid())) {
194: obj.setGuid(guidGenerator.nextGUID());
195: }
196:
197: obj.setContent(normalizeLineSeparator(obj.getContent()));
198: obj.setMatchHandler(normalizeLineSeparator(obj
199: .getMatchHandler()));
200: obj.setFormHandlerCode(normalizeLineSeparator(obj
201: .getFormHandlerCode()));
202:
203: if (isUpdate) {
204: int numRows = engine.updateXmenu(obj);
205: if (numRows != 1) {
206: return "Could not update menu";
207: }
208: } else {
209: int numRows = engine.insertXmenu(obj);
210: if (numRows != 1) {
211: return "Could not insert menu";
212: }
213: }
214:
215: String referrer = getReferrer(request);
216: if (referrer == null) {
217: if (isUpdate) {
218: referrer = "get/view.html?id=" + obj.getId();
219: } else if (obj.getParent() == null) {
220: referrer = "listRoots/list.html";
221: } else {
222: referrer = "get/view.html?id=" + obj.getParent();
223: }
224: }
225:
226: return new SimpleRedirect(referrer, "Menu "
227: + (isUpdate ? "updated" : "inserted"));
228: }
229:
230: public static String normalizeLineSeparator(String oldContent) {
231: // Removing escaped carriage returns.
232: if (isBlank(oldContent)) {
233: return oldContent;
234: }
235:
236: String lineSeparator = System.getProperty("line.separator");
237: StringBuffer newContent = new StringBuffer();
238: int crIdx = oldContent.indexOf(lineSeparator);
239: if (crIdx == -1) {
240: return oldContent;
241: }
242:
243: while (oldContent != null && crIdx != -1) {
244: newContent.append(oldContent.substring(0, crIdx));
245: newContent.append("\n");
246: int beginIndex = crIdx + lineSeparator.length();
247: if (beginIndex == oldContent.length()) {
248: oldContent = null;
249: } else {
250: oldContent = oldContent.substring(beginIndex);
251: crIdx = oldContent.indexOf(lineSeparator);
252: }
253: }
254:
255: if (oldContent != null) {
256: newContent.append(oldContent);
257: }
258:
259: return newContent.toString();
260: }
261:
262: /**
263: * Deletes Group by "id" parameter.
264: * @param request
265: * @param response
266: * @param servlet
267: * @return
268: * @throws SQLException
269: * @throws ConfigurationException
270: */
271: public Object delete(HttpServletRequest request,
272: HttpServletResponse response, ActionServlet servlet)
273: throws SQLException {
274: String id = request.getParameter("id");
275: if (id == null) {
276: return "id parameter is missing";
277: }
278:
279: getEngine(request).deleteXmenu(Integer.parseInt(id));
280:
281: String referrer = getReferrer(request);
282: if (referrer == null) {
283: referrer = "listRoots/list.html";
284: }
285:
286: return new SimpleRedirect(referrer, "Menu deleted");
287: }
288:
289: /**
290: * @param request
291: * @param response
292: * @param servlet
293: * @return
294: * @throws SQLException
295: * @throws ConfigurationException
296: */
297: public Object getPermissions(HttpServletRequest request,
298: HttpServletResponse response, ActionServlet servlet)
299: throws SQLException, ConfigurationException {
300: Map ret = new HashMap();
301: SecurityEngine engine = (SecurityEngine) getGlobal(request,
302: "db/SecurityEngine");
303: String menuId = request.getParameter("id");
304: if (menuId == null) {
305: return "Menu id parameter is not set";
306: }
307: ret.put("id", menuId);
308: SQLProcessor sqlProcessor = (SQLProcessor) getGlobal(request,
309: "sql-processor");
310: ret.put("assigned-permissions", new MenuEngine(sqlProcessor)
311: .getPermission(Integer.parseInt(menuId)));
312: ret.put("permissions", engine.getApplicationPermission());
313: ret.put("classes", new PermissionEngine(sqlProcessor)
314: .getClasses());
315: return ret;
316: }
317:
318: /**
319: * Iterates over permissions parameter values and updates database.
320: * @param request
321: * @param response
322: * @param servlet
323: * @return
324: * @throws SQLException
325: * @throws ConfigurationException
326: */
327: public Object updatePermissions(HttpServletRequest request,
328: HttpServletResponse response, ActionServlet servlet)
329: throws SQLException, ConfigurationException {
330: MenuEngine engine = getEngine(request);
331: int id = Integer.parseInt(request.getParameter("id"));
332: engine.deleteXmenuPermissionByMenu(id);
333: String[] values = request.getParameterValues("Permission");
334: if (values != null) {
335: for (int i = 0; i < values.length; i++) {
336: int pid = Integer.parseInt(values[i]);
337: engine.insertXmenuPermission(id, pid);
338: }
339: }
340:
341: return new SimpleRedirect("../" + VIEW_PATH + id,
342: "Permissions updated");
343: }
344:
345: /**
346: * Returns list of menus for AJAX tree.
347: * @param request
348: * @param response
349: * @param servlet
350: * @return
351: * @throws SQLException
352: * @throws
353: * @throws SQLException
354: * @throws ConfigurationException
355: */
356: public Object getMenu(HttpServletRequest request,
357: HttpServletResponse response, ActionServlet servlet)
358: throws SQLException {
359: String parent = request.getParameter("parentId");
360: MenuEngine engine = getEngine(request);
361: Map ret = new HashMap();
362: if (parent == null) {
363: String targetForm = request.getParameter("targetForm");
364: if (targetForm != null) {
365: ret.put("targetForm", targetForm);
366: }
367:
368: String targetField = request.getParameter("targetField");
369: if (targetField != null) {
370: ret.put("targetField", targetField);
371: }
372:
373: String pathElementId = request
374: .getParameter("pathElementId");
375: if (pathElementId != null) {
376: ret.put("pathElementId", pathElementId);
377: }
378:
379: ret.put("items", engine.getRootMenus(new ArrayList()));
380: } else {
381: ret.put("items", engine.getMenuWithChildCount(Integer
382: .parseInt(parent), new ArrayList()));
383: ret.put("path", path(parent, engine));
384: }
385: return ret;
386: }
387:
388: public String path(HttpServletRequest request,
389: HttpServletResponse response, ActionServlet servlet)
390: throws NumberFormatException, SQLException {
391: String idStr = request.getParameter("id");
392: if (idStr == null) {
393: return "Id parameter is missing";
394: }
395:
396: MenuEngine engine = getEngine(request);
397: return path(idStr, engine);
398: }
399:
400: public static String path(String idStr, MenuEngine engine)
401: throws SQLException {
402: Set ids = new HashSet();
403: Xmenu menu = engine.getXmenu(Integer.parseInt(idStr));
404: if (menu == null) {
405: return "Invalid menu id";
406: }
407: String ret = null;
408:
409: while (menu != null) {
410: if (ids.add(new Integer(menu.getId()))) {
411: if (ret == null) {
412: ret = menu.getName();
413: } else {
414: ret = menu.getName() + " / " + ret;
415: }
416:
417: if (menu.getParent() != null) {
418: menu = engine.getXmenu(menu.getParent().intValue());
419: }
420: } else {
421: break;
422: }
423: }
424: return ret;
425: }
426: }
|