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.impl.portlet.state;
023:
024: import org.jboss.portal.common.value.Value;
025: import org.jboss.portal.common.util.TypedMap;
026: import org.jboss.portal.portlet.state.AbstractPropertyMap;
027: import org.jboss.portal.portlet.state.PropertyMap;
028: import org.jboss.portal.portlet.state.producer.PortletState;
029: import org.jboss.portal.portlet.state.producer.PortletStateContext;
030:
031: import java.util.Calendar;
032: import java.util.Date;
033: import java.util.HashMap;
034: import java.util.HashSet;
035: import java.util.Iterator;
036: import java.util.Map;
037: import java.util.Set;
038:
039: /**
040: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
041: * @version $Revision: 8951 $
042: */
043: public class PersistentPortletState implements PortletStateContext {
044:
045: /** The primary key. */
046: protected Long key;
047:
048: /** The portlet id. */
049: protected String portletId;
050:
051: /** The different entries. */
052: protected Map entries;
053:
054: /** When the state has been created. */
055: protected Date creationTime;
056:
057: /** When the state expires, a null value means there is no expiration date scheduled. */
058: protected Date terminationTime;
059:
060: /** For now a registration id, later probably a one to many relationship with a registration entry. */
061: protected PersistentRegistration relatedRegistration;
062:
063: /** The clones of this state. */
064: protected Set children;
065:
066: /** The state that we cloned from. */
067: protected PersistentPortletState parent;
068:
069: /** . */
070: private PortletState ctx;
071:
072: public PersistentPortletState() {
073: this .key = null;
074: this .portletId = null;
075: this .entries = null;
076: this .creationTime = null;
077: this .terminationTime = null;
078: this .relatedRegistration = null;
079: this .children = null;
080: this .parent = null;
081: this .ctx = null;
082: }
083:
084: public PersistentPortletState(String portletId,
085: PropertyMap propertyMap) {
086: this .key = null;
087: this .portletId = portletId;
088: this .entries = new HashMap();
089: this .creationTime = Calendar.getInstance().getTime();
090: this .terminationTime = null;
091: this .relatedRegistration = null;
092: this .children = new HashSet();
093: this .parent = null;
094: this .ctx = null;
095:
096: //
097: for (Iterator i = propertyMap.keySet().iterator(); i.hasNext();) {
098: String key = (String) i.next();
099: Value value = propertyMap.getProperty(key);
100: entries.put(key,
101: new PersistentPortletStateEntry(key, value));
102: }
103: }
104:
105: public Long getKey() {
106: return key;
107: }
108:
109: public void setKey(Long key) {
110: this .key = key;
111: }
112:
113: public String getId() {
114: return key.toString();
115: }
116:
117: public String getPortletId() {
118: return portletId;
119: }
120:
121: public void setPortletId(String portletId) {
122: this .portletId = portletId;
123: }
124:
125: public Map getEntries() {
126: return entries;
127: }
128:
129: public void setEntries(Map entries) {
130: this .entries = entries;
131: }
132:
133: public Date getCreationTime() {
134: return creationTime;
135: }
136:
137: public void setCreationTime(Date creationTime) {
138: this .creationTime = creationTime;
139: }
140:
141: public Date getTerminationTime() {
142: return terminationTime;
143: }
144:
145: public void setTerminationTime(Date terminationTime) {
146: this .terminationTime = terminationTime;
147: }
148:
149: public PersistentRegistration getRelatedRegistration() {
150: return relatedRegistration;
151: }
152:
153: public void setRelatedRegistration(
154: PersistentRegistration relatedRegistration) {
155: this .relatedRegistration = relatedRegistration;
156: }
157:
158: public Set getChildren() {
159: return children;
160: }
161:
162: public void setChildren(Set children) {
163: this .children = children;
164: }
165:
166: public PersistentPortletState getParent() {
167: return parent;
168: }
169:
170: public void setParent(PersistentPortletState parent) {
171: this .parent = parent;
172: }
173:
174: //
175:
176: public PortletState getState() {
177: if (ctx == null) {
178: PropertyMap props = new AbstractPropertyMap(entries,
179: KEY_CONVERTER, VALUE_CONVERTER);
180: ctx = new PortletState(portletId, props);
181: }
182: return ctx;
183: }
184:
185: private static final TypedMap.Converter KEY_CONVERTER = new TypedMap.Converter() {
186: protected Object getInternal(Object o)
187: throws IllegalArgumentException, ClassCastException {
188: if (!(o instanceof String)) {
189: throw new ClassCastException(
190: "Was expecting an instanceof "
191: + String.class.getName() + " but was "
192: + o.getClass().getName());
193: }
194: return o;
195: }
196:
197: protected Object getExternal(Object o) {
198: if (!(o instanceof String)) {
199: throw new ClassCastException(
200: "Was expecting an instanceof "
201: + String.class.getName() + " but was "
202: + o.getClass().getName());
203: }
204: return o;
205: }
206:
207: protected boolean equals(Object o, Object o1) {
208: return o.equals(o1);
209: }
210: };
211:
212: private static TypedMap.Converter VALUE_CONVERTER = new TypedMap.Converter() {
213: protected Object getInternal(Object o)
214: throws IllegalArgumentException, ClassCastException {
215: throw new IllegalArgumentException("Immutable");
216: }
217:
218: protected Object getExternal(Object o) {
219: return ((PersistentPortletStateEntry) o).getValue();
220: }
221:
222: protected boolean equals(Object o, Object o1) {
223: return o.equals(o1);
224: }
225: };
226:
227: }
|