001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.ui.web.component.holder; 018 019import java.io.Serializable; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.Map; 023 024import javax.annotation.PostConstruct; 025import javax.annotation.PreDestroy; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029 030/** 031 * View scoped bean keeping values held by {@link UIValueHolder} component instances when their value is set. 032 * 033 * @since 6.0 034 */ 035// FIXME: annotations do not trigger registration, need to figure out why 036// @ViewScoped 037// @ManagedBean 038public class NuxeoValueHolderBean implements Serializable { 039 040 private static final long serialVersionUID = 1L; 041 042 public static final String NAME = "nuxeoValueHolderBean"; 043 044 private static final Log log = LogFactory.getLog(NuxeoValueHolderBean.class); 045 046 // map of held value by id. id is the component corresponding facelet tag 047 // id, the component facelet tag handler ensures a unique relation between 048 // the two. 049 protected Map<String, Serializable> values; 050 051 public NuxeoValueHolderBean() { 052 super(); 053 } 054 055 /** 056 * Init marked public for NXP-16182. 057 * 058 * @since 7.1 059 */ 060 @PostConstruct 061 @PreDestroy 062 public void init() { 063 values = new HashMap<>(); 064 } 065 066 public Map<String, Serializable> getValues() { 067 return Collections.unmodifiableMap(values); 068 } 069 070 public void saveState(UIValueHolder c, Object value) { 071 String fid = c.getFaceletId(); 072 if (fid == null) { 073 log.error("Cannot save UIValueHolder state: " + "missing facelet marker id on component attributes"); 074 return; 075 } 076 if (value == null || value instanceof Serializable) { 077 values.put(fid, (Serializable) value); 078 } else { 079 log.warn("Value is not serializable, cannot store it in view: " + value); 080 } 081 } 082 083 public void saveState(UIValueHolder c) { 084 saveState(c, c.getValueToExpose()); 085 } 086 087 public Object getState(String id) { 088 return values.get(id); 089 } 090 091 /** 092 * Returns true if bean holds a value for given id. 093 * 094 * @since 7.2 095 */ 096 public boolean hasState(String id) { 097 return values.containsKey(id); 098 } 099 100}