001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ResultStates.java 3720 2007-04-27 10:58:07Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.tools.UniqueIDGenerator;
011: import java.io.Serializable;
012: import java.util.LinkedHashMap;
013: import java.util.Map;
014: import java.util.Set;
015:
016: public class ResultStates implements Serializable, Cloneable {
017: private static final long serialVersionUID = 3452384106266423281L;
018:
019: static final String ID_PREFIX = "resultstates\u0000";
020:
021: private String mId;
022: private Map<String, ElementResultState> mResultStates = new LinkedHashMap<String, ElementResultState>();
023: private long mModified = System.currentTimeMillis();
024:
025: public ResultStates() {
026: regenerateId();
027: }
028:
029: public ResultStates(ResultStates states) {
030: mId = states.getId();
031: putAll(states);
032: }
033:
034: public synchronized void regenerateId() {
035: mId = ID_PREFIX + UniqueIDGenerator.generate().toString();
036: }
037:
038: public synchronized void putAll(ResultStates states) {
039: mResultStates.putAll(states.mResultStates);
040: mModified = System.currentTimeMillis();
041: }
042:
043: public synchronized void put(ElementResultState state) {
044: mResultStates.put(state.getContextId(), state);
045: mModified = System.currentTimeMillis();
046: }
047:
048: public String getId() {
049: return mId;
050: }
051:
052: public long getModified() {
053: return mModified;
054: }
055:
056: public synchronized Set<Map.Entry<String, ElementResultState>> entrySet() {
057: return mResultStates.entrySet();
058: }
059:
060: public int size() {
061: return mResultStates.size();
062: }
063:
064: public ElementResultState get(String contextId) {
065: return mResultStates.get(contextId);
066: }
067:
068: public synchronized ElementResultState remove(String contextId) {
069: return mResultStates.remove(contextId);
070: }
071:
072: public ResultStates cloneForStateStore(StateStore store) {
073: ResultStates new_resultstate = new ResultStates();
074:
075: // clone the result states
076: new_resultstate.mResultStates = new LinkedHashMap<String, ElementResultState>();
077: // get the type that the result states should be in for this state store
078: Class result_state_type = store.getResultStateType();
079: // iterate over all the result states of the original object and check if
080: // they are in the correct type so that the state store can understand them
081: for (ElementResultState result_state : mResultStates.values()) {
082: if (result_state_type.isAssignableFrom(result_state
083: .getClass())) {
084: new_resultstate.mResultStates.put(result_state
085: .getContextId(), result_state);
086: }
087: // if the type isn't compatible, convert it to the correct one
088: else {
089: ElementResultState new_result_state = store
090: .createNewResultState(result_state
091: .getContextId());
092: new_result_state.populate(result_state);
093: new_resultstate.mResultStates.put(new_result_state
094: .getContextId(), new_result_state);
095: }
096: }
097: new_resultstate.mModified = mModified;
098: new_resultstate.mId = mId;
099:
100: return new_resultstate;
101: }
102:
103: public String toString() {
104: StringBuilder result = new StringBuilder();
105: for (Map.Entry<String, ElementResultState> result_state : mResultStates
106: .entrySet()) {
107: result.append(result_state.getKey());
108: result.append(" : ");
109: result.append("contid=");
110: result.append(result_state.getValue().getContinuationId());
111: result.append(", preservedinputs=");
112: Map<String, String[]> preserved = result_state.getValue()
113: .getPreservedInputs();
114: if (preserved != null) {
115: result.append("{");
116: boolean first_entry = true;
117: for (Map.Entry<String, String[]> input : preserved
118: .entrySet()) {
119: if (!first_entry) {
120: result.append(",");
121: }
122: result.append(input.getKey());
123: result.append("=");
124: if (input.getValue() != null) {
125: result.append("[");
126: boolean first_value = true;
127: for (String value : input.getValue()) {
128: if (!first_value) {
129: result.append(",");
130: }
131: result.append(value);
132: first_value = false;
133: }
134: result.append("]");
135: } else {
136: result.append("null");
137: }
138: first_entry = false;
139: }
140: result.append("}");
141: } else {
142: result.append("null");
143: }
144: }
145:
146: return result.toString();
147: }
148: }
|