001: /*
002: * Created on 13.07.2005 for PIROL
003: *
004: * SVN header information:
005: * $Author: javamap $
006: * $Rev: 856 $
007: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
008: * $Id: MetaInformationHandler.java 856 2007-06-19 04:15:27Z javamap $
009: */
010: package de.fho.jump.pirol.utilities.metaData;
011:
012: import java.util.HashMap;
013: import java.util.Set;
014:
015: import com.vividsolutions.jump.feature.FeatureCollection;
016: import com.vividsolutions.jump.workbench.model.Layer;
017:
018: import de.fho.jump.pirol.utilities.FeatureCollection.PirolFeatureCollection;
019: import de.fho.jump.pirol.utilities.FeatureCollection.PirolFeatureCollectionRole;
020: import de.fho.jump.pirol.utilities.FeatureCollection.PirolFeatureCollectionRoleTypes;
021: import de.fho.jump.pirol.utilities.FeatureCollection.RoleStandardFeatureCollection;
022: import de.fho.jump.pirol.utilities.apiTools.HandlerToMakeYourLifeEasier;
023: import de.fho.jump.pirol.utilities.debugOutput.DebugUserIds;
024: import de.fho.jump.pirol.utilities.debugOutput.PersonalLogger;
025:
026: /**
027: * Tool class for easier handling of meta information on a layer basis.<br>
028: * - objects will be created, if neccessary<br>
029: * - you don't need to access the properties map of the data source (where the meta information is stored) yourself<br>
030: *
031: * @author Ole Rahn
032: * <br>
033: * <br>FH Osnabrück - University of Applied Sciences Osnabrück,
034: * <br>Project: PIROL (2005),
035: * <br>Subproject: Daten- und Wissensmanagement
036: *
037: * @version $Rev: 856 $
038: * @see de.fhOsnabrueck.jump.pirol.utilities.metaData.MetaInformationKeys
039: *
040: */
041: public class MetaInformationHandler implements
042: HandlerToMakeYourLifeEasier {
043: protected ObjectContainingMetaInformation objectWithMetaInformation = null;
044:
045: protected PersonalLogger logger = new PersonalLogger(
046: DebugUserIds.ALL);
047:
048: /**
049: *
050: *@param layerWithMetaInformation the layer you want the meta information of (has to have a DataSource!!)
051: */
052: public MetaInformationHandler(Layer layerWithMetaInformation) {
053: super ();
054: if (layerWithMetaInformation != null) {
055: FeatureCollection fc = layerWithMetaInformation
056: .getFeatureCollectionWrapper().getUltimateWrappee();
057:
058: if (!PirolFeatureCollection.class.isInstance(fc)) {
059: fc = createPirolFeatureCollection(fc);
060: layerWithMetaInformation.setFeatureCollection(fc);
061: }
062:
063: this .objectWithMetaInformation = (PirolFeatureCollection) fc;
064: } else {
065: throw new RuntimeException("given layer is null.");
066: }
067: }
068:
069: /**
070: *
071: *@param objectWithMetaInformation the object you want the meta information of
072: */
073: public MetaInformationHandler(
074: ObjectContainingMetaInformation objectWithMetaInformation) {
075: super ();
076: if (objectWithMetaInformation != null) {
077: this .objectWithMetaInformation = objectWithMetaInformation;
078: } else {
079: throw new RuntimeException("given layer is null.");
080: }
081: }
082:
083: /**
084: * creates a PirolFeatureCollection out of a regular FeatureCollection
085: *@param fc regular FeatureCollection
086: *@return PirolFeatureCollection
087: */
088: public static final PirolFeatureCollection createPirolFeatureCollection(
089: FeatureCollection fc) {
090: return MetaInformationHandler.createPirolFeatureCollection(fc,
091: new RoleStandardFeatureCollection());
092: }
093:
094: /**
095: * creates a PirolFeatureCollection out of a regular FeatureCollection
096: *@param fc regular FeatureCollection
097: *@return PirolFeatureCollection
098: */
099: public static final PirolFeatureCollection createPirolFeatureCollection(
100: FeatureCollection fc, PirolFeatureCollectionRole role) {
101: PirolFeatureCollection pfc = null;
102:
103: if (!PirolFeatureCollection.class.isInstance(fc)) {
104: pfc = new PirolFeatureCollection(fc, role);
105: } else {
106: pfc = (PirolFeatureCollection) fc;
107:
108: if (!role
109: .equalsRole(PirolFeatureCollectionRoleTypes.STANDARD)) {
110: pfc.addRole(role);
111: }
112:
113: }
114:
115: return pfc;
116: }
117:
118: /**
119: * Retrieve the existent meta information map.
120: *@return the existent meta information map or null, if there is none
121: */
122: public MetaDataMap getExistentMetaInformationMap() {
123: if (this .containsMetaInformation()) {
124: return this .getMetaInformationMap();
125: }
126: return null;
127: }
128:
129: /**
130: * Retrieve the existent meta information map or create one.
131: *@return the existent meta information map or an empty meta information map (that is now attached to the DataSource)
132: *@throws RuntimeException, if the given DataSource doesn't even have properties (<code>getProperties()</code>)
133: */
134: public MetaDataMap getMetaInformationMap() {
135: if (!this .containsMetaInformation()) {
136: MetaDataMap newMap = new MetaDataMap();
137:
138: if (this .objectWithMetaInformation != null) {
139: this .logger.printDebug("creating new meta map for "
140: + this .objectWithMetaInformation);
141: this .objectWithMetaInformation
142: .setMetaInformation(newMap);
143: } else
144: return null;
145:
146: return newMap;
147: } else if (this .objectWithMetaInformation != null) {
148: return this .objectWithMetaInformation.getMetaInformation();
149: }
150:
151: return null;
152: }
153:
154: /**
155: * @return true if the given layer already contains meta information, false if not
156: */
157: public boolean containsMetaInformation() {
158: return (this .objectWithMetaInformation != null && this .objectWithMetaInformation
159: .getMetaInformation() != null);
160: }
161:
162: /**
163: * Adds a new meta information key-value-pair to the meta information map, replaces
164: * an existing pair with the same key.
165: *@param key
166: *@param value
167: */
168: public void addMetaInformation(String key, Object value) {
169: MetaDataMap metaMap = this .getMetaInformationMap();
170: if (metaMap.containsKey(key))
171: metaMap.remove(key);
172: metaMap.addMetaInformation(key, value);
173: }
174:
175: public HashMap getMetaData() {
176: MetaDataMap metaMap = this .getMetaInformationMap();
177: return metaMap.getMetaData();
178: }
179:
180: public void setMetaData(HashMap<Object, Object> metaData) {
181: MetaDataMap metaMap = this .getMetaInformationMap();
182: metaMap.setMetaData(metaData);
183: }
184:
185: public void clear() {
186: MetaDataMap metaMap = this .getMetaInformationMap();
187: metaMap.clear();
188: }
189:
190: public boolean containsKey(String key) {
191: MetaDataMap metaMap = this .getMetaInformationMap();
192: return metaMap.containsKey(key);
193: }
194:
195: public Object getMetaInformation(String key) {
196: MetaDataMap metaMap = this .getMetaInformationMap();
197: return metaMap.get(key);
198: }
199:
200: public boolean containsValue(Object value) {
201: MetaDataMap metaMap = this .getMetaInformationMap();
202: return metaMap.containsValue(value);
203: }
204:
205: public Set keySet() {
206: MetaDataMap metaMap = this .getMetaInformationMap();
207: return metaMap.keySet();
208: }
209:
210: public Object remove(String key) {
211: MetaDataMap metaMap = this .getMetaInformationMap();
212: return metaMap.remove(key);
213: }
214:
215: // Specific information
216:
217: /**
218: * @return the Attribute2UnitMap of the given DataSource or null, if there is none
219: */
220: public Attribute2UnitMap getAttribute2UnitMap() {
221: if (this .containsAttribute2UnitMap())
222: return (Attribute2UnitMap) this
223: .getMetaInformation(Attribute2UnitMap.KEY_ATTRIBUTE2UNIT);
224: return null;
225: }
226:
227: public void putAttribute2UnitMap(Attribute2UnitMap attribute2UnitMap) {
228: this .addMetaInformation(Attribute2UnitMap.KEY_ATTRIBUTE2UNIT,
229: attribute2UnitMap);
230: }
231:
232: public boolean containsAttribute2UnitMap() {
233: return this.containsKey(Attribute2UnitMap.KEY_ATTRIBUTE2UNIT);
234: }
235:
236: }
|