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.jetspeed.om.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.Locale;
023:
024: import org.apache.commons.collections.map.MultiValueMap;
025: import org.apache.jetspeed.om.common.GenericMetadata;
026: import org.apache.jetspeed.om.common.LocalizedField;
027:
028: /**
029: * GenericMetadataImpl
030: * <br/>
031: * Implementation that allows retrieving localized information
032: *
033: * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>
034: * @version $Id: GenericMetadataImpl.java 551866 2007-06-29 12:15:40Z ate $
035: *
036: */
037: public abstract class GenericMetadataImpl implements GenericMetadata {
038: private Collection fields = null;
039: private transient MultiValueMap fieldMap = null;
040:
041: private MultiValueMap getFieldMap(boolean create) {
042: if (fieldMap == null && create) {
043: synchronized (this ) {
044: if (fieldMap == null) {
045: fieldMap = new MultiValueMap();
046: }
047: }
048: }
049: return fieldMap;
050: }
051:
052: /* (non-Javadoc)
053: * @see org.apache.jetspeed.om.common.GenericMetadata#addField(java.util.Locale, java.lang.String, java.lang.String)
054: */
055: public void addField(Locale locale, String name, String value) {
056: LocalizedField field = createLocalizedField();
057: field.setName(name);
058: field.setValue(value);
059: field.setLocale(locale);
060:
061: addField(field);
062: }
063:
064: /* (non-Javadoc)
065: * @see org.apache.jetspeed.om.common.GenericMetadata#addField(org.apache.jetspeed.om.common.LocalizedField)
066: */
067: public void addField(LocalizedField field) {
068: if (fields == null) {
069: fields = new ArrayList();
070: }
071:
072: fields.add(field);
073: getFieldMap(true).put(field.getName(), field);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.apache.jetspeed.om.common.GenericMetadata#getFields(java.lang.String)
078: */
079: public Collection getFields(String name) {
080: //TODO: return an immutable version?
081: MultiValueMap fieldMap = getFieldMap(false);
082: return (Collection) (fieldMap != null ? fieldMap.get(name)
083: : null);
084: }
085:
086: /* (non-Javadoc)
087: * @see org.apache.jetspeed.om.common.GenericMetadata#setFields(java.lang.String, java.util.Collection)
088: */
089: public void setFields(String name, Collection values) {
090: MultiValueMap fieldMap = getFieldMap(false);
091: if (fieldMap != null) {
092: fieldMap.remove(name);
093: }
094:
095: Iterator fieldIter = fields.iterator();
096: while (fieldIter.hasNext()) {
097: LocalizedField field = (LocalizedField) fieldIter.next();
098: if (field != null && field.getName() != null
099: && field.getName().equals(name)) {
100: fieldIter.remove();
101: }
102: }
103:
104: if (values != null) {
105: Iterator iter = values.iterator();
106: while (iter.hasNext()) {
107: LocalizedField field = (LocalizedField) iter.next();
108: getFieldMap(true).put(field.getName(), field);
109: }
110:
111: fields.addAll(values);
112: }
113: }
114:
115: /* (non-Javadoc)
116: * @see org.apache.jetspeed.om.common.GenericMetadata#getFields()
117: */
118: public Collection getFields() {
119: return fields;
120: }
121:
122: /* (non-Javadoc)
123: * @see org.apache.jetspeed.om.common.GenericMetadata#setFields(java.util.Collection)
124: */
125: public void setFields(Collection fields) {
126: this .fields = fields;
127:
128: MultiValueMap fieldMap = getFieldMap(false);
129: if (fieldMap != null) {
130: fieldMap.clear();
131: }
132:
133: if (fields != null) {
134: Iterator fieldIter = fields.iterator();
135: while (fieldIter.hasNext()) {
136: LocalizedField field = (LocalizedField) fieldIter
137: .next();
138: if (field.getName() != null) {
139: getFieldMap(true).put(field.getName(), field);
140: }
141: }
142: }
143:
144: }
145:
146: /* (non-Javadoc)
147: * @see org.apache.jetspeed.om.common.GenericMetadata#copyFields(java.util.Collection)
148: */
149: public void copyFields(Collection fields) {
150: // preserve matching fields during copy to
151: // minimize persistent store thrash and
152: // field uniqueness constraint violations
153: // that may occur if identical field is
154: // removed and reinserted
155: if ((this .fields != null) && !this .fields.isEmpty()) {
156: // remove unique existing fields
157: if (fields != null) {
158: this .fields.retainAll(fields);
159: } else {
160: this .fields = null;
161: }
162: }
163: if ((fields != null) && !fields.isEmpty()) {
164: // create new fields collection if necessary
165: if (this .fields == null) {
166: this .fields = new ArrayList();
167: }
168: // copy unique new metadata members
169: Iterator fieldIter = fields.iterator();
170: while (fieldIter.hasNext()) {
171: LocalizedField field = (LocalizedField) fieldIter
172: .next();
173: if (!this .fields.contains(field)) {
174: addField(field.getLocale(), field.getName(), field
175: .getValue());
176: }
177: }
178: }
179:
180: // update field map
181: MultiValueMap fieldMap = getFieldMap(false);
182: if (fieldMap != null) {
183: fieldMap.clear();
184: }
185:
186: if (this .fields != null) {
187: Iterator fieldIter = this .fields.iterator();
188: while (fieldIter.hasNext()) {
189: LocalizedField field = (LocalizedField) fieldIter
190: .next();
191: getFieldMap(true).put(field.getName(), field);
192: }
193: }
194: }
195: }
|