001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.ui.web.component.holder; 020 021import java.io.Serializable; 022import java.util.Collections; 023import java.util.HashMap; 024import java.util.Map; 025 026import javax.annotation.PostConstruct; 027import javax.annotation.PreDestroy; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031 032/** 033 * View scoped bean keeping values held by {@link UIValueHolder} component instances when their value is set. 034 * 035 * @since 6.0 036 */ 037// FIXME: annotations do not trigger registration, need to figure out why 038// @ViewScoped 039// @ManagedBean 040public class NuxeoValueHolderBean implements Serializable { 041 042 private static final long serialVersionUID = 1L; 043 044 public static final String NAME = "nuxeoValueHolderBean"; 045 046 private static final Log log = LogFactory.getLog(NuxeoValueHolderBean.class); 047 048 // map of held value by id. id is the component corresponding facelet tag 049 // id, the component facelet tag handler ensures a unique relation between 050 // the two. 051 protected Map<String, Serializable> values; 052 053 public NuxeoValueHolderBean() { 054 super(); 055 } 056 057 /** 058 * Init marked public for NXP-16182. 059 * 060 * @since 7.1 061 */ 062 @PostConstruct 063 @PreDestroy 064 public void init() { 065 values = new HashMap<>(); 066 } 067 068 public Map<String, Serializable> getValues() { 069 return Collections.unmodifiableMap(values); 070 } 071 072 public void saveState(UIValueHolder c, Object value) { 073 String fid = c.getFaceletId(); 074 if (fid == null) { 075 log.error("Cannot save UIValueHolder state: " + "missing facelet marker id on component attributes"); 076 return; 077 } 078 if (value == null || value instanceof Serializable) { 079 values.put(fid, (Serializable) value); 080 } else { 081 log.warn("Value is not serializable, cannot store it in view: " + value); 082 } 083 } 084 085 public void saveState(UIValueHolder c) { 086 saveState(c, c.getValueToExpose()); 087 } 088 089 public Object getState(String id) { 090 return values.get(id); 091 } 092 093 /** 094 * Returns true if bean holds a value for given id. 095 * 096 * @since 7.2 097 */ 098 public boolean hasState(String id) { 099 return values.containsKey(id); 100 } 101 102}