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.sesame;
30  
31  import java.beans.PropertyDescriptor;
32  import java.lang.reflect.Method;
33  import java.util.HashSet;
34  import java.util.Set;
35  
36  import org.openrdf.elmo.ElmoProperty;
37  import org.openrdf.elmo.ElmoPropertyFactory;
38  import org.openrdf.elmo.annotations.inverseOf;
39  import org.openrdf.elmo.annotations.localized;
40  import org.openrdf.elmo.annotations.oneOf;
41  import org.openrdf.elmo.annotations.rdf;
42  import org.openrdf.elmo.impl.UnmodifiableElmoProperty;
43  import org.openrdf.elmo.sesame.helpers.PropertyChanger;
44  import org.openrdf.elmo.sesame.roles.SesameEntity;
45  import org.openrdf.model.Literal;
46  import org.openrdf.model.URI;
47  import org.openrdf.model.Value;
48  import org.openrdf.model.ValueFactory;
49  import org.openrdf.model.impl.ValueFactoryImpl;
50  
51  /**
52   * Creates {@link ElmoProperty} objects for a given predicate.
53   * 
54   * @author James Leigh
55   * 
56   * @param <E>
57   *            property type
58   */
59  public class SesamePropertyFactory<E> implements ElmoPropertyFactory<E> {
60  	private static ValueFactory vf = new ValueFactoryImpl();
61  
62  	private URI predicate;
63  
64  	private boolean inverse;
65  
66  	private boolean localized;
67  
68  	private boolean readOnly;
69  
70  	private Set<? extends Value> oneOf;
71  
72  	private PropertyChanger inferencer;
73  
74  	public static ValueFactory getValueFactory() {
75  		return vf;
76  	}
77  
78  	public URI getPredicate() {
79  		return predicate;
80  	}
81  
82  	public Set<? extends Value> getOneOf() {
83  		return oneOf;
84  	}
85  
86  	public SesamePropertyFactory<E> setPropertyDescriptor(
87  			PropertyDescriptor property) {
88  		String uri;
89  		Method getter = property.getReadMethod();
90  		rdf rdf = getter.getAnnotation(rdf.class);
91  		inverseOf inv = getter.getAnnotation(inverseOf.class);
92  		if (rdf != null && rdf.value().length > 0) {
93  			uri = rdf.value()[0];
94  			inverse = false;
95  		} else if (inv != null && inv.value().length > 0) {
96  			uri = inv.value()[0];
97  			inverse = true;
98  		} else {
99  			throw new IllegalArgumentException(
100 					"Property has no Elmo annotations on the getter method");
101 		}
102 		predicate = vf.createURI(uri);
103 		localized = getter.isAnnotationPresent(localized.class);
104 		Method setter = property.getWriteMethod();
105 		readOnly = setter == null;
106 		if (setter != null && setter.isAnnotationPresent(oneOf.class)) {
107 			oneOf = convertOneOf(setter.getAnnotation(oneOf.class));
108 		}
109 		inferencer = getPropertyChanger();
110 		return this;
111 	}
112 
113 	public ElmoProperty<E> createElmoProperty(Object bean) {
114 		assert bean instanceof SesameEntity : bean;
115 		SesameProperty<E> property = createSesameProperty((SesameEntity) bean);
116 		if (readOnly)
117 			return new UnmodifiableElmoProperty<E>(property);
118 		return property;
119 	}
120 
121 	protected PropertyChanger getPropertyChanger() {
122 		return new PropertyChanger(predicate, getOneOf());
123 	}
124 
125 	private Set<? extends Value> convertOneOf(oneOf of) {
126 		if (of.datatype().length() > 0) {
127 			Set<Literal> set = new HashSet<Literal>();
128 			URI datatype = vf.createURI(of.datatype());
129 			for (String label : of.label()) {
130 				set.add(vf.createLiteral(label, datatype));
131 			}
132 			return set;
133 		}
134 		Set<URI> set = new HashSet<URI>();
135 		for (String uri : of.value()) {
136 			set.add(vf.createURI(uri));
137 		}
138 		return set;
139 	}
140 
141 	@SuppressWarnings("unchecked")
142 	private SesameProperty<E> createSesameProperty(SesameEntity bean) {
143 		if (localized)
144 			return (SesameProperty<E>) new LocalizedSesameProperty(bean,
145 					inferencer);
146 		if (inverse)
147 			return new InverseSesameProperty<E>(bean, inferencer);
148 		return new SesameProperty<E>(bean, inferencer);
149 	}
150 
151 }