001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.admin.ui;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.common.value.Value;
026: import org.jboss.portal.core.model.instance.Instance;
027: import org.jboss.portal.core.model.instance.InstanceContainer;
028: import org.jboss.portal.core.model.instance.InstanceDefinition;
029: import org.jboss.portal.core.model.instance.NoSuchInstanceException;
030: import org.jboss.portal.identity.RoleModule;
031: import org.jboss.portal.portlet.Portlet;
032: import org.jboss.portal.portlet.PortletInvokerException;
033: import org.jboss.portal.portlet.info.ModeInfo;
034: import org.jboss.portal.portlet.info.PortletInfo;
035: import org.jboss.portal.portlet.info.PreferenceInfo;
036: import org.jboss.portal.portlet.info.PreferencesInfo;
037: import org.jboss.portal.portlet.state.PropertyChange;
038: import org.jboss.portal.portlet.state.PropertyMap;
039: import org.jboss.portal.security.spi.provider.DomainConfigurator;
040:
041: import javax.faces.application.FacesMessage;
042: import javax.faces.context.ExternalContext;
043: import javax.faces.context.FacesContext;
044: import javax.faces.event.ActionEvent;
045: import javax.faces.model.SelectItem;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.Comparator;
050: import java.util.HashMap;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Map;
054: import java.util.Set;
055:
056: /**
057: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
058: * @author <a href="mailto:boleslaw dot dawidowicz at jboss.org">Boleslaw Dawidowicz</a>
059: * @version $Revision: 9332 $
060: */
061: public class InstanceManagerBean {
062:
063: /** . */
064: private final Mode INTERNAL_EDIT_CONTENT = Mode
065: .create("edit_content");
066:
067: // Wired services
068:
069: /** . */
070: private InstanceContainer instanceContainer;
071:
072: /** . */
073: private RoleModule roleModule;
074:
075: /** . */
076: private DomainConfigurator domainConfigurator;
077:
078: // Navigational state of the user
079:
080: /** . */
081: private String selectedId;
082:
083: /** . */
084: private String selectedPlugin;
085:
086: /** . */
087: private int selectedFrom;
088:
089: /** . */
090: private Integer selectedRow;
091:
092: /** . */
093: private int paginationSize;
094:
095: // Runtime fields depending on the navigational state
096:
097: /** . */
098: private PreferencesBean selectedPrefs;
099:
100: /** . */
101: private Instance selectedInstance;
102:
103: /** . */
104: private List instances;
105:
106: /** . */
107: private AuthorizationBean auth = new InstanceAuthorizationBean();
108:
109: // Services accessors
110:
111: public RoleModule getRoleModule() {
112: return roleModule;
113: }
114:
115: public void setRoleModule(RoleModule roleModule) {
116: this .roleModule = roleModule;
117: }
118:
119: public InstanceContainer getInstanceContainer() {
120: return instanceContainer;
121: }
122:
123: public void setInstanceContainer(InstanceContainer instanceContainer) {
124: this .instanceContainer = instanceContainer;
125: }
126:
127: public DomainConfigurator getDomainConfigurator() {
128: return domainConfigurator;
129: }
130:
131: public void setDomainConfigurator(
132: DomainConfigurator domainConfigurator) {
133: this .domainConfigurator = domainConfigurator;
134: }
135:
136: // Navigational state accessor
137:
138: public int getPaginationSize() {
139: return paginationSize;
140: }
141:
142: public void setPaginationSize(int paginationSize) {
143: this .paginationSize = paginationSize;
144: }
145:
146: public int getSelectedFrom() {
147: return selectedFrom;
148: }
149:
150: public void setSelectedFrom(int selectedFrom) {
151: this .selectedFrom = selectedFrom;
152: }
153:
154: public Integer getSelectedRow() {
155: return selectedRow;
156: }
157:
158: public void setSelectedRow(Integer selectedRow) {
159: this .selectedRow = selectedRow;
160: }
161:
162: public String getSelectedId() {
163: return selectedId;
164: }
165:
166: public void setSelectedId(String selectedId) {
167: this .selectedId = selectedId;
168: int index = getInstances().indexOf(getSelectedInstance());
169: if (index != -1) {
170: selectedRow = new Integer(index);
171: selectedFrom = (index / paginationSize) * paginationSize;
172: }
173: }
174:
175: public String getSelectedPlugin() {
176: return selectedPlugin;
177: }
178:
179: public void setSelectedPlugin(String selectedPlugin) {
180: this .selectedPlugin = selectedPlugin;
181: }
182:
183: // Runtime fields
184:
185: public Instance getSelectedInstance() {
186: if (selectedInstance == null && selectedId != null) {
187: selectedInstance = instanceContainer
188: .getDefinition(selectedId);
189: }
190: return selectedInstance;
191: }
192:
193: public PreferencesBean getSelectedPrefs() {
194: Instance selectedInstance = getSelectedInstance();
195:
196: //
197: if (selectedPrefs == null && selectedInstance != null) {
198: try {
199: selectedPrefs = new PreferencesBean(true);
200:
201: //
202: PreferencesInfo prefsInfo = selectedInstance
203: .getPortlet().getInfo().getPreferences();
204: Set keys = prefsInfo.getKeys();
205: PropertyMap props = selectedInstance.getProperties();
206: for (Iterator i = keys.iterator(); i.hasNext();) {
207: String key = (String) i.next();
208: PreferenceInfo prefInfo = prefsInfo
209: .getPreference(key);
210: Value value = props.getProperty(key);
211: selectedPrefs.addEntry(prefInfo, value);
212: }
213: } catch (PortletInvokerException e) {
214: e.printStackTrace();
215: }
216: }
217:
218: //
219: return selectedPrefs;
220: }
221:
222: public AuthorizationBean getAuth() {
223: return auth;
224: }
225:
226: private List getInstances() {
227: if (instances == null) {
228: // Remove content editors
229: List tmpInstances = new ArrayList();
230: nextInstance: for (Iterator i = instanceContainer
231: .getDefinitions().iterator(); i.hasNext();) {
232: Instance instance = (Instance) i.next();
233:
234: //
235: try {
236: // Filter portlets that are editors
237: Portlet portlet = instance.getPortlet();
238: PortletInfo info = portlet.getInfo();
239: for (Iterator j = info.getCapabilities()
240: .getAllModes().iterator(); j.hasNext();) {
241: ModeInfo modeInfo = (ModeInfo) j.next();
242: if (modeInfo.getMode().equals(
243: INTERNAL_EDIT_CONTENT)) {
244: continue nextInstance;
245: }
246: }
247: //
248: tmpInstances.add(instance);
249: } catch (PortletInvokerException ignore) {
250: }
251: }
252: Collections.sort(tmpInstances, INSTANCE_COMPARATOR);
253: instances = tmpInstances;
254: }
255: //
256: return instances;
257: }
258:
259: public Collection getSelectedInstances() {
260: List list = getInstances();
261:
262: //
263: int to = Math.min(selectedFrom + paginationSize, list.size());
264:
265: //
266: return list.subList(selectedFrom, to);
267: }
268:
269: public Map getSelectedInstancesPrefs() {
270: Map map = new HashMap();
271: Collection instances = getSelectedInstances();
272: Iterator it = instances.iterator();
273:
274: while (it.hasNext()) {
275: InstanceDefinition instanceDef = (InstanceDefinition) it
276: .next();
277: try {
278: map.put(instanceDef.getId(), instanceDef
279: .getProperties());
280: } catch (PortletInvokerException e) {
281: // TODO Auto-generated catch block
282: e.printStackTrace();
283: }
284: }
285:
286: return map;
287: }
288:
289: public int getInstanceCount() {
290: return getInstances().size();
291: }
292:
293: /** Refresh the selected prefs. */
294: public void refresh() {
295: selectedInstance = null;
296: instances = null;
297: }
298:
299: // UI operations
300:
301: public void selectFrom() {
302: Map pmap = FacesContext.getCurrentInstance()
303: .getExternalContext().getRequestParameterMap();
304: selectedFrom = Integer.parseInt((String) pmap.get("from"));
305: selectedPlugin = null;
306: selectedId = null;
307: }
308:
309: public void selectPlugin() {
310: Map pmap = FacesContext.getCurrentInstance()
311: .getExternalContext().getRequestParameterMap();
312: selectedPlugin = (String) pmap.get("plugin");
313: }
314:
315: public void selectInstance() {
316: FacesContext ctx = FacesContext.getCurrentInstance();
317: ExternalContext ectx = ctx.getExternalContext();
318: Map params = ectx.getRequestParameterMap();
319: selectedId = (String) params.get("id");
320: selectedPlugin = (String) params.get("plugin");
321: selectedPrefs = null;
322: }
323:
324: public void selectInstance(ActionEvent e) {
325: selectInstance();
326: }
327:
328: public void deleteInstance(ActionEvent ae) {
329: try {
330: String id = (String) ae.getComponent().getAttributes().get(
331: "instanceId");
332:
333: if (id != null) {
334: //
335: instanceContainer.destroyDefinition(id);
336:
337: //
338: selectedId = null;
339: selectedPrefs = null;
340: selectedFrom = 0;
341: selectedPlugin = null;
342: }
343: } catch (NoSuchInstanceException e) {
344: e.printStackTrace();
345: } catch (PortletInvokerException e) {
346: e.printStackTrace();
347: }
348: }
349:
350: public void updatePrefs() {
351: try {
352: List tmp = new ArrayList();
353: List entries = selectedPrefs.getEntries();
354: for (int i = 0; i < entries.size(); i++) {
355: PreferenceBean entry = (PreferenceBean) entries.get(i);
356: if (entry.isStale()) {
357: PropertyChange change = PropertyChange.newUpdate(
358: entry.getName(), entry.getValue());
359: tmp.add(change);
360: }
361: }
362: PropertyChange[] changes = (PropertyChange[]) tmp
363: .toArray(new PropertyChange[tmp.size()]);
364: getSelectedInstance().setProperties(changes);
365:
366: // Todo handle that change was ok in the UI
367: } catch (PortletInvokerException e) {
368: // Todo handle issue in the UI
369: e.printStackTrace();
370: }
371:
372: //
373: selectedId = null;
374: selectedPrefs = null;
375: selectedPlugin = null;
376: }
377:
378: public void cancelPrefs() {
379: selectedId = null;
380: selectedPrefs = null;
381: selectedPlugin = null;
382: }
383:
384: /** A comparator for portlets. */
385: static final Comparator INSTANCE_COMPARATOR = new Comparator() {
386: public int compare(Object o1, Object o2) {
387: Instance i1 = (Instance) o1;
388: Instance i2 = (Instance) o2;
389: return i1.getId().compareToIgnoreCase(i2.getId());
390: }
391: };
392:
393: public class InstanceAuthorizationBean extends AuthorizationBean {
394:
395: public DomainConfigurator getDomainConfigurator() {
396: return InstanceManagerBean.this .getDomainConfigurator();
397: }
398:
399: protected String getURI() {
400: Instance instance = getSelectedInstance();
401: if (instance != null) {
402: return instance.getId();
403: } else {
404: return null;
405: }
406: }
407:
408: public String submit() {
409: String stringMessage = "Security has been correctly updated";
410: FacesMessage message = new FacesMessage(
411: FacesMessage.SEVERITY_INFO, stringMessage,
412: stringMessage);
413: FacesContext.getCurrentInstance().addMessage("status",
414: message);
415: selectedId = null;
416: selectedPlugin = null;
417: return null;
418: }
419:
420: public String cancel() {
421: selectedId = null;
422: selectedPlugin = null;
423: return null;
424: }
425:
426: public RoleModule getRoleModule() {
427: return roleModule;
428: }
429:
430: public SelectItem[] getAvailableActions() {
431: return new SelectItem[] { new SelectItem("view", "View"),
432: new SelectItem("admin", "Admin") };
433: }
434: }
435: }
|