001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/user/tags/sakai_2-4-1/user-tool-admin-prefs/admin-prefs/src/java/org/sakaiproject/user/tool/AdminPrefsTool.java $
003: * $Id: AdminPrefsTool.java 8355 2006-04-26 19:50:28Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.user.tool;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Vector;
026:
027: import javax.faces.application.FacesMessage;
028: import javax.faces.context.FacesContext;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.sakaiproject.entity.api.ResourceProperties;
033: import org.sakaiproject.entity.api.ResourcePropertiesEdit;
034: import org.sakaiproject.exception.IdUnusedException;
035: import org.sakaiproject.tool.api.SessionManager;
036: import org.sakaiproject.user.api.PreferencesEdit;
037: import org.sakaiproject.user.api.PreferencesService;
038:
039: /**
040: * <p>
041: * AdminPrefsTool is a Sakai Admin tool to view and edit anyone's preferences.
042: * </p>
043: */
044: public class AdminPrefsTool {
045: /**
046: * Represents a name value pair in a keyed preferences set.
047: */
048: public class KeyNameValue {
049: /** Is this value a list?. */
050: protected boolean m_isList = false;
051:
052: /** The key. */
053: protected String m_key = null;
054:
055: /** The name. */
056: protected String m_name = null;
057:
058: /** The original is this value a list?. */
059: protected boolean m_origIsList = false;
060:
061: /** The original key. */
062: protected String m_origKey = null;
063:
064: /** The original name. */
065: protected String m_origName = null;
066:
067: /** The original value. */
068: protected String m_origValue = null;
069:
070: /** The value. */
071: protected String m_value = null;
072:
073: public KeyNameValue(String key, String name, String value,
074: boolean isList) {
075: m_key = key;
076: m_origKey = key;
077: m_name = name;
078: m_origName = name;
079: m_value = value;
080: m_origValue = value;
081: m_isList = isList;
082: m_origIsList = isList;
083: }
084:
085: public String getKey() {
086: return m_key;
087: }
088:
089: public String getName() {
090: return m_name;
091: }
092:
093: public String getOrigKey() {
094: return m_origKey;
095: }
096:
097: public String getOrigName() {
098: return m_origName;
099: }
100:
101: public String getOrigValue() {
102: return m_origValue;
103: }
104:
105: public String getValue() {
106: return m_value;
107: }
108:
109: public boolean isChanged() {
110: return ((!m_name.equals(m_origName))
111: || (!m_value.equals(m_origValue))
112: || (!m_key.equals(m_origKey)) || (m_isList != m_origIsList));
113: }
114:
115: public boolean isList() {
116: return m_isList;
117: }
118:
119: public boolean origIsList() {
120: return m_origIsList;
121: }
122:
123: public void setKey(String value) {
124: if (!m_key.equals(value)) {
125: m_key = value;
126: }
127: }
128:
129: public void setList(boolean b) {
130: m_isList = b;
131: }
132:
133: public void setName(String value) {
134: if (!m_name.equals(value)) {
135: m_name = value;
136: }
137: }
138:
139: public void setValue(String value) {
140: if (!m_value.equals(value)) {
141: m_value = value;
142: }
143: }
144: }
145:
146: /** Our log (commons). */
147: private static Log M_log = LogFactory.getLog(AdminPrefsTool.class);
148:
149: /** The PreferencesEdit being worked on. */
150: protected PreferencesEdit m_edit = null;
151:
152: /** Preferences service (injected dependency) */
153: protected PreferencesService m_preferencesService = null;
154:
155: /** Session manager (injected dependency) */
156: protected SessionManager m_sessionManager = null;
157:
158: /** The PreferencesEdit in KeyNameValue collection form. */
159: protected Collection m_stuff = null;
160:
161: /** The user id (from the end user) to edit. */
162: protected String m_userId = null;
163:
164: /**
165: * no-arg constructor.
166: */
167: public AdminPrefsTool() {
168: M_log.info("constructed");
169: }
170:
171: /**
172: * Cancel the edit and cleanup.
173: */
174: protected void cancelEdit() {
175: // cancel
176: m_preferencesService.cancel(m_edit);
177:
178: // cleanup
179: m_stuff = null;
180: m_edit = null;
181: }
182:
183: /**
184: * Access the prefs being edited, in KeyNameValue collection form.
185: *
186: * @return a collection of name,value options for the current configuration.
187: */
188: public Collection getPreferences() {
189: // make sure we have this setup
190: setupEdit();
191:
192: return m_stuff;
193: }
194:
195: /**
196: * Access the user id of the preferences being edited.
197: *
198: * @return The user id of the preferences being edited.
199: */
200: public String getUserId() {
201: return m_userId;
202: }
203:
204: /**
205: * Process the add command from the edit view.
206: *
207: * @return navigation outcome:
208: */
209: public String processActionAdd() {
210: if (M_log.isDebugEnabled())
211: M_log.debug("save");
212:
213: // save
214: m_stuff.add(new KeyNameValue("", "", "", false));
215:
216: return null;
217: }
218:
219: /**
220: * Process the cancel command from the edit view.
221: *
222: * @return navigation outcome:
223: */
224: public String processActionCancel() {
225: if (M_log.isDebugEnabled())
226: M_log.debug("cancel");
227:
228: // cancel
229: cancelEdit();
230:
231: return "list";
232: }
233:
234: /**
235: * Process the edit command.
236: *
237: * @return navigation outcome:
238: */
239: public String processActionEdit() {
240: if (M_log.isDebugEnabled())
241: M_log.debug("processActionEdit");
242:
243: if (getUserId() == null) {
244: FacesContext.getCurrentInstance().addMessage(null,
245: new FacesMessage("Please enter a user id."));
246: return null;
247: }
248:
249: try {
250: m_edit = m_preferencesService.edit(getUserId());
251: return "edit";
252: } catch (IdUnusedException e) {
253: try {
254: m_edit = m_preferencesService.add(getUserId());
255: return "edit";
256: } catch (Exception ee) {
257: FacesContext.getCurrentInstance().addMessage(null,
258: new FacesMessage(ee.toString()));
259: return null;
260: }
261: } catch (Exception e) {
262: FacesContext.getCurrentInstance().addMessage(null,
263: new FacesMessage(e.toString()));
264: return null;
265: }
266: }
267:
268: /**
269: * Process the save command from the edit view.
270: *
271: * @return navigation outcome:
272: */
273: public String processActionSave() {
274: if (M_log.isDebugEnabled())
275: M_log.debug("save");
276:
277: // save
278: saveEdit();
279:
280: return "list";
281: }
282:
283: /**
284: * Save any changed values from the edit and cleanup.
285: */
286: protected void saveEdit() {
287: // move the stuff from m_stuff into the edit
288: for (Iterator i = m_stuff.iterator(); i.hasNext();) {
289: KeyNameValue knv = (KeyNameValue) i.next();
290: if (knv.isChanged()) {
291: // find the original to remove (unless this one was new)
292: if (!knv.getOrigKey().equals("")) {
293: ResourcePropertiesEdit props = m_edit
294: .getPropertiesEdit(knv.getOrigKey());
295: props.removeProperty(knv.getOrigName());
296: }
297:
298: // add the new if we have a key and name and value
299: if ((!knv.getKey().equals(""))
300: && (!knv.getName().equals(""))
301: && (!knv.getValue().equals(""))) {
302: ResourcePropertiesEdit props = m_edit
303: .getPropertiesEdit(knv.getKey());
304:
305: if (knv.isList()) {
306: // split by ", "
307: String[] parts = knv.getValue().split(", ");
308: for (int p = 0; p < parts.length; p++) {
309: props.addPropertyToList(knv.getName(),
310: parts[p]);
311: }
312: } else {
313: props
314: .addProperty(knv.getName(), knv
315: .getValue());
316: }
317: }
318: }
319: }
320:
321: // save the preferences, release the edit
322: m_preferencesService.commit(m_edit);
323:
324: m_stuff = null;
325: m_edit = null;
326: }
327:
328: /**
329: * Set the preferences service.
330: *
331: * @param mgr
332: * The preferences service.
333: */
334: public void setPreferencesService(PreferencesService mgr) {
335: m_preferencesService = mgr;
336: }
337:
338: /**
339: * Set the session manager.
340: *
341: * @param mgr
342: * The session manager.
343: */
344: public void setSessionManager(SessionManager mgr) {
345: m_sessionManager = mgr;
346: }
347:
348: /**
349: * If we have not yet setup our m_stuff edit buffer, do so.
350: */
351: protected void setupEdit() {
352: if (m_stuff != null)
353: return;
354:
355: Vector rv = new Vector();
356:
357: if (m_edit != null) {
358: Collection keys = m_edit.getKeys();
359: for (Iterator i = keys.iterator(); i.hasNext();) {
360: String key = (String) i.next();
361: ResourceProperties props = m_edit.getProperties(key);
362: for (Iterator names = props.getPropertyNames(); names
363: .hasNext();) {
364: String name = (String) names.next();
365: String value = props.getPropertyFormatted(name);
366:
367: List values = props.getPropertyList(name);
368: boolean isList = values.size() > 1;
369:
370: rv.add(new KeyNameValue(key, name, value, isList));
371: }
372: }
373: }
374:
375: m_stuff = rv;
376: }
377:
378: /**
379: * Set the user id to edit.
380: *
381: * @param id
382: * The user id to edit.
383: */
384: public void setUserId(String id) {
385: m_userId = id;
386: }
387: }
|