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 java.io.Serializable;
036: import java.util.ArrayList;
037: import java.util.List;
038:
039: /**
040: * Content Container for the Cache to keep all versions for an id
041: *
042: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
043: */
044: public class FxCachedContentContainer implements Serializable {
045: private static final long serialVersionUID = -6810666943342672605L;
046: private long id;
047: private int maxVersion;
048: private int liveVersion;
049: private List<FxCachedContent> content;
050:
051: /**
052: * Ctor
053: *
054: * @param content the content to cache (and its security info)
055: */
056: public FxCachedContentContainer(FxCachedContent content) {
057: this .id = content.getContent().getId();
058: this .maxVersion = content.getContent().getMaxVersion();
059: this .liveVersion = content.getContent().getLiveVersion();
060: this .content = new ArrayList<FxCachedContent>(maxVersion);
061: this .content.add(content);
062: }
063:
064: /**
065: * Getter for the id
066: *
067: * @return id
068: */
069: public long getId() {
070: return id;
071: }
072:
073: /**
074: * Getter for the max. version number
075: *
076: * @return max. version number
077: */
078: public int getMaxVersion() {
079: return maxVersion;
080: }
081:
082: /**
083: * Getter for the live version number
084: *
085: * @return live version number
086: */
087: public int getLiveVersion() {
088: return liveVersion;
089: }
090:
091: /**
092: * Try to get a content by its primary key, will return <code>null</code> if not found
093: *
094: * @param pk primary key
095: * @return content
096: */
097: public synchronized FxCachedContent get(FxPK pk) {
098: for (FxCachedContent found : content) {
099: if (pk.isDistinctVersion()) {
100: if (found.getContent().getVersion() == pk.getVersion())
101: return found;
102: } else {
103: if (pk.getVersion() == FxPK.MAX
104: && found.getContent().isMaxVersion())
105: return found;
106: else if (pk.getVersion() == FxPK.LIVE
107: && found.getContent().isLiveVersion())
108: return found;
109: }
110: }
111: return null;
112: }
113:
114: /**
115: * Add a content to the cache if - and only if - the id matches and its not contained already
116: *
117: * @param content the content to add
118: */
119: public synchronized void add(FxCachedContent content) {
120: if (this.id == content.getContent().getId()
121: && get(content.getContent().getPk()) == null) {
122: this.content.add(content);
123: this.maxVersion = content.getContent().getMaxVersion();
124: this.liveVersion = content.getContent().getLiveVersion();
125: }
126: }
127: }
|