View Javadoc

1   /**
2    * Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.socialsignin.spring.data.dynamodb.mapping;
17  
18  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
19  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
20  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBVersionAttribute;
21  import org.springframework.data.annotation.Id;
22  import org.springframework.data.annotation.Reference;
23  import org.springframework.data.annotation.Transient;
24  import org.springframework.data.mapping.Association;
25  import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
26  import org.springframework.data.mapping.model.Property;
27  import org.springframework.data.mapping.model.SimpleTypeHolder;
28  
29  import java.lang.annotation.Annotation;
30  import java.util.Collection;
31  import java.util.Collections;
32  import java.util.HashSet;
33  import java.util.Set;
34  
35  /**
36   * {@link DynamoDBPersistentProperty} implementation
37   * 
38   * @author Michael Lavelle
39   * @author Sebastian Just
40   */
41  class DynamoDBPersistentPropertyImpl extends AnnotationBasedPersistentProperty<DynamoDBPersistentProperty>
42  		implements
43  			DynamoDBPersistentProperty {
44  
45  	private static final Collection<Class<? extends Annotation>> ASSOCIATION_ANNOTATIONS;
46  	private static final Collection<Class<? extends Annotation>> ID_ANNOTATIONS;
47  
48  	static {
49  
50  		Set<Class<? extends Annotation>> annotations = new HashSet<Class<? extends Annotation>>();
51  
52  		annotations.add(Reference.class); // Reference not yet supported
53  		ASSOCIATION_ANNOTATIONS = Collections.unmodifiableSet(annotations);
54  
55  		annotations = new HashSet<>();
56  		annotations.add(Id.class);
57  		annotations.add(DynamoDBHashKey.class);
58  		ID_ANNOTATIONS = annotations;
59  	}
60  
61  	/**
62  	 * Creates a new {@link DynamoDBPersistentPropertyImpl}
63  	 * 
64  	 * @param property
65  	 *            must not be {@literal null}.
66  	 * @param owner
67  	 *            must not be {@literal null}.
68  	 * @param simpleTypeHolder
69  	 *            must not be {@literal null}.
70  	 */
71  
72  	public DynamoDBPersistentPropertyImpl(Property property, DynamoDBPersistentEntityImpl<?> owner,
73  			SimpleTypeHolder simpleTypeHolder) {
74  		super(property, owner, simpleTypeHolder);
75  	}
76  
77  	@Override
78  	public boolean isWritable() {
79  		return !isAnnotationPresent(DynamoDBIgnore.class);
80  	}
81  
82  	public boolean isHashKeyProperty() {
83  		return isAnnotationPresent(DynamoDBHashKey.class);
84  	}
85  
86  	public boolean isCompositeIdProperty() {
87  		return isAnnotationPresent(Id.class);
88  	}
89  
90  	/*
91  	 * (non-Javadoc)
92  	 * 
93  	 * @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty
94  	 * #isIdProperty()
95  	 */
96  	@Override
97  	public boolean isIdProperty() {
98  
99  		for (Class<? extends Annotation> annotation : ID_ANNOTATIONS) {
100 			if (isAnnotationPresent(annotation)) {
101 				return true;
102 			}
103 		}
104 
105 		return false;
106 	}
107 
108 	/*
109 	 * (non-Javadoc)
110 	 * 
111 	 * @see
112 	 * org.springframework.data.mapping.model.AbstractPersistentProperty#isEntity ()
113 	 */
114 	// @Override
115 
116 	public boolean isEntity() {
117 
118 		return isAnnotationPresent(Reference.class);// Reference not Yet
119 		// Supported
120 		// return propertyDescriptor != null
121 		// && propertyDescriptor.getPropertyType().isAnnotationPresent(
122 		// DynamoDBTable.class);
123 
124 		// return false;
125 
126 	}
127 
128 	/*
129 	 * (non-Javadoc)
130 	 * 
131 	 * @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty
132 	 * #isAssociation()
133 	 */
134 	@Override
135 	public boolean isAssociation() {
136 
137 		for (Class<? extends Annotation> annotationType : ASSOCIATION_ANNOTATIONS) {
138 			if (findAnnotation(annotationType) != null) {
139 				// No query lookup yet supported ( see
140 				// Repositories.getPersistentEntity(..) )
141 				// return !information.isCollectionLike();
142 				return true;
143 			}
144 		}
145 
146 		return false;
147 	}
148 
149 	/*
150 	 * (non-Javadoc)
151 	 * 
152 	 * @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty
153 	 * #isTransient()
154 	 */
155 	@Override
156 	public boolean isTransient() {
157 		return isAnnotationPresent(Transient.class) || super.isTransient() || isAnnotationPresent(DynamoDBIgnore.class);
158 	}
159 
160 	@Override
161 	public boolean isVersionProperty() {
162 		return super.isVersionProperty() || isAnnotationPresent(DynamoDBVersionAttribute.class);
163 	}
164 
165 	/*
166 	 * (non-Javadoc)
167 	 * 
168 	 * @see org.springframework.data.mapping.model.AbstractPersistentProperty#
169 	 * createAssociation()
170 	 */
171 	@Override
172 	protected Association<DynamoDBPersistentProperty> createAssociation() {
173 		return new Association<DynamoDBPersistentProperty>(this, null);
174 	}
175 }