001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.content;
034:
035: import com.flexive.shared.FxContext;
036: import com.flexive.shared.security.LifeCycleInfo;
037:
038: import java.io.Serializable;
039: import java.util.HashMap;
040: import java.util.Hashtable;
041: import java.util.Map;
042:
043: /**
044: * Information about a content's existing versions
045: *
046: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
047: */
048: public class FxContentVersionInfo implements Serializable {
049:
050: private static final long serialVersionUID = -2928402301816290488L;
051: private long id;
052: private int minVersion;
053: private int maxVersion;
054: private int liveVersion;
055: private int lastModifiedVersion;
056: private Map<Integer, LifeCycleInfo> versions;
057: private VersionSelector versionSelector;
058:
059: /**
060: * Selector for versions (helps with EL)
061: */
062: public static final class VersionSelector extends
063: Hashtable<Integer, LifeCycleInfo> {
064: FxContentVersionInfo versionInfo;
065:
066: /**
067: * Ctor
068: *
069: * @param versionInfo versionInfo to use
070: */
071: VersionSelector(FxContentVersionInfo versionInfo) {
072: this .versionInfo = versionInfo;
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: @Override
079: public LifeCycleInfo get(Object key) {
080: return versionInfo.versions.get(key);
081: }
082: }
083:
084: /**
085: * Ctor
086: *
087: * @param id instance id
088: * @param minVersion minimum existing version
089: * @param maxVersion maximum existing version
090: * @param liveVersion live version (if not exists: -1)
091: * @param lastModifiedVersion version that has the latest modification date
092: * @param versions map of version,LifeCycleInfo entries
093: */
094: public FxContentVersionInfo(long id, int minVersion,
095: int maxVersion, int liveVersion, int lastModifiedVersion,
096: Map<Integer, LifeCycleInfo> versions) {
097: this .id = id;
098: this .minVersion = minVersion;
099: this .maxVersion = maxVersion;
100: this .liveVersion = liveVersion;
101: this .lastModifiedVersion = lastModifiedVersion;
102: this .versions = versions;
103: this .versionSelector = new VersionSelector(this );
104: }
105:
106: /**
107: * Get the id of the instance
108: *
109: * @return id of the instance
110: */
111: public long getId() {
112: return id;
113: }
114:
115: /**
116: * Get the minimum existing version
117: *
118: * @return minimum existing version
119: */
120: public int getMinVersion() {
121: return minVersion;
122: }
123:
124: /**
125: * Get the maximum existing version
126: *
127: * @return maximum existing version
128: */
129: public int getMaxVersion() {
130: return maxVersion;
131: }
132:
133: /**
134: * Get the live version. if no live version exists then <code>-1</code> is returned
135: *
136: * @return live version or <code>-1</code>
137: */
138: public int getLiveVersion() {
139: return liveVersion;
140: }
141:
142: /**
143: * Does a live version exist for this content instance
144: *
145: * @return if a live version exists for this content instance
146: */
147: public boolean hasLiveVersion() {
148: return liveVersion > 0;
149: }
150:
151: /**
152: * Does a live version exist for this content instance
153: * (EL compatible variant)
154: *
155: * @return if a live version exists for this content instance
156: * @see #hasLiveVersion()
157: */
158: public boolean isHasLiveVersion() {
159: return liveVersion > 0;
160: }
161:
162: /**
163: * Get the version that was changed most recently
164: *
165: * @return version that was changed most recently
166: */
167: public int getLastModifiedVersion() {
168: return lastModifiedVersion;
169: }
170:
171: /**
172: * Get an iterator for all available versions
173: *
174: * @return iterator for all available versions
175: */
176: public Integer[] getVersions() {
177: return versions.keySet().toArray(
178: new Integer[versions.keySet().size()]);
179: }
180:
181: /**
182: * How many versions exist?
183: *
184: * @return number of versions
185: */
186: public int getVersionCount() {
187: return versions.size();
188: }
189:
190: /**
191: * Get the version selector
192: *
193: * @return VersionSelector
194: */
195: public VersionSelector getVersionSelector() {
196: return versionSelector;
197: }
198:
199: /**
200: * Get the LifeCycleInfo for a requested version or <code>null</code> if the
201: * requested version does not exist
202: *
203: * @param version the requested version
204: * @return LifeCycleInfo for a requested version or <code>null</code> if the
205: * requested version does not exist
206: */
207: public LifeCycleInfo getLifeCycleInfo(int version) {
208: return versions.get(version);
209: }
210:
211: @Override
212: public String toString() {
213: StringBuilder sb = new StringBuilder(200);
214: sb.append("ContentVersionInfo:\nGeneral information: {id=")
215: .append(this .id).append(",minVer=").append(
216: this .minVersion).append(",maxVer=").append(
217: this .maxVersion).append(",liveVer=").append(
218: this .liveVersion).append(",lastModVer=")
219: .append(this .lastModifiedVersion).append("}\n");
220: for (int v : versions.keySet())
221: sb.append("Version ").append(v).append(":").append(
222: versions.get(v).toString()).append("\n");
223:
224: return sb.toString();
225: }
226:
227: /**
228: * Check if the requested Pk's version exists
229: *
230: * @param pk primary key
231: * @return if the requested Pk's version exists
232: */
233: public boolean containsVersion(FxPK pk) {
234: if (!pk.isDistinctVersion()) {
235: return pk.getVersion() == FxPK.MAX
236: || pk.getVersion() == FxPK.LIVE && liveVersion > 0;
237: }
238: return versions.containsKey(pk.getVersion());
239: }
240:
241: /**
242: * Get the correct maximum or live version if <code>version</code> is version constant from FxPK
243: *
244: * @param version the requested version
245: * @return distinct version
246: */
247: public int getDistinctVersion(int version) {
248: if (version == FxPK.MAX)
249: return maxVersion;
250: if (version == FxPK.LIVE)
251: return liveVersion;
252: return version;
253: }
254:
255: /**
256: * Temp. class to create LifeCycleInfo's for new instances
257: */
258: static class NewLifeCycleInfoImpl implements LifeCycleInfo {
259:
260: private long userId;
261: private long time;
262:
263: /**
264: * Ctor
265: *
266: * @param userId user id to use
267: */
268: NewLifeCycleInfoImpl(long userId) {
269: this .userId = userId;
270: this .time = System.currentTimeMillis();
271: }
272:
273: /**
274: * {@inheritDoc}
275: */
276: public long getCreatorId() {
277: return userId;
278: }
279:
280: /**
281: * {@inheritDoc}
282: */
283: public long getCreationTime() {
284: return time;
285: }
286:
287: /**
288: * {@inheritDoc}
289: */
290: public long getModificatorId() {
291: return userId;
292: }
293:
294: /**
295: * {@inheritDoc}
296: */
297: public long getModificationTime() {
298: return time;
299: }
300: }
301:
302: /**
303: * Create an empty version info for new FxContent instances using the calling user as creator
304: *
305: * @return an empty version info for new FxContent instances
306: */
307: public static FxContentVersionInfo createEmpty() {
308: Map<Integer, LifeCycleInfo> versions = new HashMap<Integer, LifeCycleInfo>(
309: 1);
310: versions.put(1, new NewLifeCycleInfoImpl(FxContext.get()
311: .getTicket().getUserId()));
312: return new FxContentVersionInfo(FxPK.NEW_ID, 1, 1, -1, 1,
313: versions);
314: }
315: }
|