001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.converter;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.faces.beans.SelectBean;
037: import com.flexive.faces.messages.FxFacesMsgErr;
038: import com.flexive.shared.SelectableObjectWithLabel;
039: import com.flexive.shared.SelectableObject;
040: import com.flexive.shared.scripting.FxScriptInfo;
041: import com.flexive.shared.security.ACL;
042: import com.flexive.shared.security.Mandator;
043: import com.flexive.shared.security.Role;
044: import com.flexive.shared.security.UserGroup;
045: import com.flexive.shared.structure.FxType;
046: import com.flexive.shared.tree.FxTemplateInfo;
047:
048: import javax.faces.component.UIComponent;
049: import javax.faces.context.FacesContext;
050: import javax.faces.convert.Converter;
051: import javax.faces.model.SelectItem;
052: import java.util.ArrayList;
053: import java.util.List;
054:
055: /**
056: * Basic Converter for all subclasses of SelectableObjectWithName.
057: */
058: public class SelectableObjectConverter implements Converter {
059:
060: /**
061: * Returns the selectable object.
062: *
063: * @param facesContext the context
064: * @param uiComponent the component
065: * @param selectableObjectId the class and id of the selectable object as string
066: * @return the selectable object, or null if no match was found
067: */
068: public Object getAsObject(FacesContext facesContext,
069: UIComponent uiComponent, String selectableObjectId) {
070:
071: if (selectableObjectId == null || selectableObjectId.equals("")) {
072: return null;
073: }
074:
075: // Extract value informations
076: long id;
077: Class theClass;
078: try {
079: String split[] = selectableObjectId.split(":");
080: id = Long.valueOf(split[1]);
081: theClass = Class.forName(split[0]);
082: } catch (Exception e) {
083: new FxFacesMsgErr(
084: "ex.converter.selectableObjectConverter.getAsObjectError",
085: selectableObjectId).addToContext();
086: return null;
087: }
088:
089: // Try to find the object
090: List<SelectItem> items = getOptions(theClass);
091: for (SelectItem item : items) {
092: SelectableObject so = (SelectableObject) item.getValue();
093: if (so.getId() == id)
094: return so;
095: }
096:
097: return null;
098: }
099:
100: private List<SelectItem> getOptions(Class c) {
101: SelectBean sb = (SelectBean) FxJsfUtils
102: .getManagedBean("fxSelectBean");
103: // Load the items depending on the object class
104: try {
105: if (c.equals(ACL.class)) {
106: return sb.getACLs();
107: } else if (c.equals(FxType.class)) {
108: return sb.getTypes();
109: } else if (c.equals(UserGroup.class)) {
110: return sb.getGlobalUserGroups();
111: } else if (c.equals(Mandator.class)) {
112: return sb.getMandators();
113: } else if (c.equals(Role.class)) {
114: return sb.getRoles();
115: } else if (c.equals(FxTemplateInfo.class)) {
116: return sb.getTemplates();
117: } else if (c.equals(FxScriptInfo.class)) {
118: return sb.getAllScripts();
119: } else {
120: new FxFacesMsgErr(
121: "ex.converter.selectableObjectConverter.classNotSupported",
122: c.getName() + "'").addToContext();
123: return new ArrayList<SelectItem>(0);
124: }
125: } catch (Exception t) {
126: new FxFacesMsgErr(t).addToContext();
127: return new ArrayList<SelectItem>(0);
128: }
129: }
130:
131: /**
132: * Returns the id of the selectabe object as string.
133: *
134: * @return the id of the selectabe object as string
135: */
136: public String getAsString(FacesContext facesContext,
137: UIComponent uiComponent, Object object) {
138:
139: // Null check
140: if (object == null) {
141: return null;
142: }
143:
144: if (object instanceof SelectableObject
145: && ((SelectableObject) object).getId() == -1) {
146: return null;
147: }
148:
149: // Process object
150: final List<SelectItem> items = getOptions(object.getClass());
151: if (object instanceof SelectableObjectWithLabel) {
152: final SelectableObjectWithLabel input = (SelectableObjectWithLabel) object;
153: try {
154: // Return the id of the SelectableObjectWithName
155: if (items != null) {
156: for (SelectItem item : items) {
157: SelectableObjectWithLabel so = (SelectableObjectWithLabel) item
158: .getValue();
159: if (so.equals(object)) {
160: return so.getClass().getName() + ":"
161: + so.getId();
162: }
163: }
164: }
165: return "error";
166: } catch (Exception t) {
167: new FxFacesMsgErr(
168: "ex.converter.selectableObjectConverter.getAsStringError",
169: input.getClass(), input.getId()).addToContext();
170: return "error";
171: }
172: } else if (object instanceof SelectableObject) {
173: final SelectableObject input = (SelectableObject) object;
174: try {
175: // Return the id of the SelectableObjectWithName
176: if (items != null) {
177: for (SelectItem item : items) {
178: SelectableObject so = (SelectableObject) item
179: .getValue();
180: if (so.equals(object)) {
181: return so.getClass().getName() + ":"
182: + so.getId();
183: }
184: }
185: }
186: return "error";
187: } catch (Exception t) {
188: new FxFacesMsgErr(
189: "ex.converter.selectableObjectConverter.getAsStringError",
190: input.getClass(), input.getId()).addToContext();
191: return "error";
192: }
193: } else
194: return "error";
195: }
196:
197: }
|