View Javadoc

1   /*
2    * Copyright (c) 2007, James Leigh All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.dynabean.helpers;
30  
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  import java.util.concurrent.ConcurrentHashMap;
38  import java.util.concurrent.ConcurrentMap;
39  
40  import org.apache.commons.beanutils.DynaProperty;
41  import org.openrdf.model.URI;
42  
43  /**
44   * Backing class for a SesameDynaBean to store common properties shared between
45   * beans.
46   * 
47   * @author James Leigh
48   * 
49   */
50  public class DynaClassInfo implements OntologyDigester {
51  
52  	private Collection<URI> rdfTypes;
53  
54  	private ConcurrentMap<String, URI> predicates = new ConcurrentHashMap<String, URI>();
55  
56  	private ConcurrentMap<String, Boolean> functional = new ConcurrentHashMap<String, Boolean>();
57  
58  	private ConcurrentMap<String, List<URI>> predicateRdfTypes = new ConcurrentHashMap<String, List<URI>>();
59  
60  	private ConcurrentMap<String, DynaProperty> properties = new ConcurrentHashMap<String, DynaProperty>();
61  
62  	public DynaClassInfo(Collection<URI> rdfTypes) {
63  		this.rdfTypes = rdfTypes;
64  	}
65  
66  	public String getName() {
67  		return toString();
68  	}
69  
70  	public Collection<URI> getRdfTypes() {
71  		return rdfTypes;
72  	}
73  
74  	public Collection<URI> getPredicates() {
75  		return predicates.values();
76  	}
77  
78  	public void addPredicate(URI pred) {
79  		predicates.put(pred.getLocalName(), pred);
80  	}
81  
82  	public void setFunctional(URI pred, boolean functional) {
83  		if (functional)
84  			this.functional.put(pred.getLocalName(), Boolean.TRUE);
85  	}
86  
87  	public List<URI> getRdfTypes(URI pred) {
88  		return predicateRdfTypes.get(pred.getLocalName());
89  	}
90  
91  	public void setRdfTypes(URI pred, List<URI> rdfTypes) {
92  		predicateRdfTypes.put(pred.getLocalName(), rdfTypes);
93  	}
94  
95  	public void addRdfType(URI pred, URI rdfType) {
96  		String key = pred.getLocalName();
97  		List<URI> rdfTypes = new ArrayList<URI>();
98  		List<URI> o = predicateRdfTypes.putIfAbsent(key, rdfTypes);
99  		if (o != null) {
100 			rdfTypes = o;
101 		}
102 		rdfTypes.add(rdfType);
103 	}
104 
105 	public void setIndexed(URI pred) {
106 		if (isFunctional(pred.getLocalName())) {
107 			DynaProperty dp = new DynaProperty(pred.getLocalName(), List.class);
108 			properties.put(pred.getLocalName(), dp);
109 		}
110 	}
111 
112 	public void setMapped(URI pred) {
113 		if (isFunctional(pred.getLocalName())) {
114 			DynaProperty dp = new DynaProperty(pred.getLocalName(), Map.class);
115 			properties.put(pred.getLocalName(), dp);
116 		}
117 	}
118 
119 	public void end() {
120 		for (String property : predicates.keySet()) {
121 			if (!properties.containsKey(property)) {
122 				DynaProperty dp;
123 				if (isFunctional(property))
124 					dp = new DynaProperty(property);
125 				else
126 					dp = new DynaProperty(property, Set.class);
127 				properties.put(property, dp);
128 			}
129 		}
130 	}
131 
132 	public DynaProperty getDynaProperty(String property) {
133 		if (properties.containsKey(property))
134 			return properties.get(property);
135 		throw new IllegalArgumentException("Invalid property: " + property
136 				+ ".  Valid properties are "
137 				+ new ArrayList<String>(properties.keySet()).toString());
138 	}
139 
140 	public DynaProperty[] getDynaProperties() {
141 		return properties.values().toArray(new DynaProperty[properties.size()]);
142 	}
143 
144 	public URI getPredicate(String property) {
145 		if (predicates.containsKey(property))
146 			return predicates.get(property);
147 		throw new IllegalArgumentException("Invalid property: " + property
148 				+ ".  Valid properties are "
149 				+ new ArrayList<String>(properties.keySet()).toString());
150 	}
151 
152 	public boolean isFunctional(String property) {
153 		return functional.containsKey(property);
154 	}
155 
156 	public Collection<URI> getRdfTypes(String property) {
157 		return predicateRdfTypes.get(property);
158 	}
159 
160 	@Override
161 	public boolean equals(Object obj) {
162 		if (this == obj)
163 			return true;
164 		if (!(obj instanceof DynaClassInfo))
165 			return false;
166 		DynaClassInfo o = (DynaClassInfo) obj;
167 		return rdfTypes.equals(o.rdfTypes);
168 	}
169 
170 	@Override
171 	public int hashCode() {
172 		return rdfTypes.hashCode();
173 	}
174 
175 	@Override
176 	public String toString() {
177 		Iterator<URI> i = rdfTypes.iterator();
178 		if (rdfTypes.size() == 1)
179 			return i.next().toString();
180 		StringBuilder buf = new StringBuilder();
181 		boolean hasNext = i.hasNext();
182 		while (hasNext) {
183 			String o = i.next().toString();
184 			buf.append(String.valueOf(o));
185 			hasNext = i.hasNext();
186 			if (hasNext)
187 				buf.append(";");
188 		}
189 		return buf.toString();
190 	}
191 
192 }