View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23   
24  package net.smartlab.web.registry;
25  
26  import java.util.Collection;
27  import java.util.Iterator;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionMapping;
34  import org.apache.struts.action.DynaActionForm;
35  
36  import net.smartlab.web.AbstractArchiveAction;
37  import net.smartlab.web.ActionException;
38  import net.smartlab.web.BusinessException;
39  
40  /**
41   * TODO documentation
42   * 
43   * @author rlogiacco
44   */
45  public class EntryAction extends AbstractArchiveAction {
46  
47  	/**
48  	 * TODO documentation Comment for <code>domain</code>
49  	 */
50  	private Domain domain = Domain.getInstance();
51  
52  	/**
53  	 * @see net.smartlab.web.AbstractArchiveAction#search(org.apache.struts.action.ActionForm,
54  	 *      javax.servlet.http.HttpServletRequest,
55  	 *      javax.servlet.http.HttpServletResponse,
56  	 *      org.apache.struts.action.ActionMapping)
57  	 */
58  	public String search(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ActionException {
59  		try {
60  			Collection entries = domain.listEntries(super.getSearchInfo(request));
61  			request.setAttribute("entries", entries);
62  			return "success";
63  		} catch (BusinessException be) {
64  			throw new ActionException(be.getMessage(), be.getCause());
65  		}
66  
67  	}
68  
69  	/**
70  	 * @see net.smartlab.web.AbstractArchiveAction#select(org.apache.struts.action.ActionForm,
71  	 *      javax.servlet.http.HttpServletRequest,
72  	 *      javax.servlet.http.HttpServletResponse,
73  	 *      org.apache.struts.action.ActionMapping)
74  	 */
75  	public String select(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ActionException {
76  		try {
77  			Entry entry = domain.findEntry(request.getParameter("id"));
78  			request.setAttribute("entry", entry);
79  			if (form != null && !super.hasErrors(request)) {
80  				super.reset(form, request, mapping);
81  				super.populate(form, entry, request.getLocale());
82  			}
83  			return "success";
84  		} catch (BusinessException be) {
85  			throw new ActionException(be.getMessage(), be.getCause());
86  		}
87  	}
88  
89  	/**
90  	 * @see net.smartlab.web.AbstractArchiveAction#update(org.apache.struts.action.ActionForm,
91  	 *      javax.servlet.http.HttpServletRequest,
92  	 *      javax.servlet.http.HttpServletResponse,
93  	 *      org.apache.struts.action.ActionMapping)
94  	 */
95  	public String update(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ActionException {
96  		try {
97  			Entry entry = null;
98  			if (super.isChecked("group", request)) {
99  				entry = new List();
100 				String[] entries = super.getSelection(form, "user-selection");
101 				if (entries != null) {
102 					for (int i = 0; i < entries.length; i++) {
103 						((List)entry).add(domain.findEntry(entries[i]));
104 					}
105 				}
106 			} else {
107 				// FIXME
108 				entry = null; //new Entity();
109 			}
110 			super.valorize(form, entry, request.getLocale());
111 			domain.updateEntry(entry);
112 		} catch (BusinessException be) {
113 			throw new ActionException(be.getMessage(), be.getCause());
114 		}
115 		return "success";
116 	}
117 
118 	/**
119 	 * @see net.smartlab.web.AbstractArchiveAction#remove(org.apache.struts.action.ActionForm,
120 	 *      javax.servlet.http.HttpServletRequest,
121 	 *      javax.servlet.http.HttpServletResponse,
122 	 *      org.apache.struts.action.ActionMapping)
123 	 */
124 	public String remove(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ActionException {
125 		try {
126 			domain.removeEntry(request.getParameter("id"));
127 		} catch (BusinessException be) {
128 			throw new ActionException(be.getMessage(), be.getCause());
129 		}
130 		return "success";
131 	}
132 
133 	/**
134 	 * @see net.smartlab.web.AbstractArchiveAction#removeAll(org.apache.struts.action.ActionForm,
135 	 *      javax.servlet.http.HttpServletRequest,
136 	 *      javax.servlet.http.HttpServletResponse,
137 	 *      org.apache.struts.action.ActionMapping)
138 	 */
139 	public String removeAll(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ActionException {
140 		try {
141 			String[] ids = super.getListSelection(request);
142 			for (int i = 0; i < ids.length; i++) {
143 				domain.removeEntry(ids[i]);
144 			}
145 		} catch (BusinessException be) {
146 			throw new ActionException(be.getMessage(), be.getCause());
147 		}
148 		return "success";
149 	}
150 
151 	/**
152 	 * TODO documentation
153 	 * 
154 	 * @param form
155 	 * @param request
156 	 * @param response
157 	 * @param mapping
158 	 * @return
159 	 * @throws ActionException
160 	 */
161 	public String entries(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ActionException {
162 		try {
163 			Entry entry = domain.findEntry(request.getParameter("id"));
164 			request.setAttribute("entry", entry);
165 			List list = (List)entry;
166 			Collection entries = list.getEntries();
167 			String[] selection = new String[entries.size()];
168 			Iterator selected = entries.iterator();
169 			for (int i = 0; selected.hasNext(); i++) {
170 				Entry member = (Entry)selected.next();
171 				selection[i] = Long.toString(member.getId());
172 			}
173 			((DynaActionForm)form).set("entry-selection", selection);
174 			request.setAttribute("entries", domain.listEntries(super.getSearchInfo(request)));
175 			return "success";
176 		} catch (BusinessException be) {
177 			throw new ActionException(be.getMessage(), be.getCause());
178 		}
179 	}
180 }