001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.profile.impl;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.cocoon.portal.coplet.CopletData;
024: import org.apache.cocoon.portal.util.DeltaApplicableReferencesAdjustable;
025:
026: /**
027: * Holds instances of CopletData.
028: *
029: * @author <a href="mailto:bluetkemeier@s-und-n.de">Björn Lütkemeier</a>
030: *
031: * @version CVS $Id: CopletDataManager.java 433543 2006-08-22 06:22:54Z crossley $
032: */
033: public class CopletDataManager implements
034: DeltaApplicableReferencesAdjustable {
035:
036: /**
037: * The coplet data instances.
038: */
039: private Map copletData = new HashMap();
040:
041: /**
042: * Signals whether a delta has been applied.
043: */
044: private boolean deltaApplied = false;
045:
046: /**
047: * Gets all coplet data.
048: */
049: public Map getCopletData() {
050: return this .copletData;
051: }
052:
053: /**
054: * Gets the specified coplet data.
055: */
056: public CopletData getCopletData(String name) {
057: return (CopletData) this .copletData.get(name);
058: }
059:
060: /**
061: * Puts the specified coplet data to the manager.
062: */
063: public void putCopletData(CopletData data) {
064: this .copletData.put(data.getId(), data);
065: }
066:
067: /**
068: * Applies the specified delta.
069: * @throws ClassCastException If the object is not of the expected type.
070: */
071: public boolean applyDelta(Object object) {
072: CopletDataManager manager = (CopletDataManager) object;
073:
074: this .deltaApplied = true;
075:
076: Iterator iterator = manager.getCopletData().values().iterator();
077: CopletData data, delta;
078: while (iterator.hasNext()) {
079: delta = (CopletData) iterator.next();
080: data = this .getCopletData(delta.getId());
081: if (data == null) {
082: this .putCopletData(delta);
083: } else {
084: data.applyDelta(delta);
085: }
086: }
087:
088: return true;
089: }
090:
091: /**
092: * Checks if a delta has been applied.
093: */
094: public boolean deltaApplied() {
095: return this .deltaApplied;
096: }
097:
098: /**
099: * Updates the references to contained DeltaApplicable objects
100: * if no delta has been applied to them.
101: * @throws ClassCastException If the object is not of the expected type.
102: */
103: public void adjustReferences(Object object) {
104: CopletDataManager manager = (CopletDataManager) object;
105:
106: Iterator iterator = this .copletData.values().iterator();
107: CopletData data, other;
108: while (iterator.hasNext()) {
109: data = (CopletData) iterator.next();
110: if (!data.deltaApplied()) {
111: other = manager.getCopletData(data.getId());
112: if (other != null) {
113: this.putCopletData(other);
114: }
115: }
116: }
117: }
118: }
|