/
ODataEntity.java
184 lines (160 loc) · 5.48 KB
/
ODataEntity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.windowsazure.services.media.implementation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import com.microsoft.windowsazure.services.media.implementation.atom.ContentType;
import com.microsoft.windowsazure.services.media.implementation.atom.EntryType;
import com.microsoft.windowsazure.services.media.implementation.atom.LinkType;
import com.microsoft.windowsazure.services.media.implementation.content.Constants;
import com.microsoft.windowsazure.services.media.models.LinkInfo;
import com.microsoft.windowsazure.services.media.models.ListResult;
/**
* Class wrapping deserialized OData entities. Allows easy
* access to entry and content types.
*
*/
public abstract class ODataEntity<T> {
private final EntryType entry;
private final T content;
protected ODataEntity(EntryType entry, T content) {
this.entry = entry;
this.content = content;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected ODataEntity(T content) {
this.content = content;
ContentType contentElement = new ContentType();
contentElement.getContent().add(
new JAXBElement(Constants.ODATA_PROPERTIES_ELEMENT_NAME, content.getClass(), content));
entry = new EntryType();
entry.getEntryChildren().add(
new JAXBElement(Constants.ATOM_CONTENT_ELEMENT_NAME, ContentType.class, contentElement));
}
/**
* @return the entry
*/
protected EntryType getEntry() {
return entry;
}
/**
* @return the content
*/
protected T getContent() {
return content;
}
/**
* Test if the entity contains a link with the given rel attribute.
*
* @param rel
* Rel of link to check for
* @return True if link is found, false if not.
*/
public boolean hasLink(String rel) {
return getLink(rel) != null;
}
/**
* Get the link with the given rel attribute
*
* @param rel
* rel of link to retrieve
* @return The link if found, null if not.
*/
public LinkInfo getLink(String rel) {
for (Object child : entry.getEntryChildren()) {
LinkType link = LinkFromChild(child);
if (link != null && link.getRel().equals(rel)) {
return new LinkInfo(link);
}
}
return null;
}
/**
* Return the links from this entry
*
* @return List of the links.
*/
public List<LinkInfo> getLinks() {
ArrayList<LinkInfo> links = new ArrayList<LinkInfo>();
for (Object child : entry.getEntryChildren()) {
LinkType link = LinkFromChild(child);
if (link != null) {
links.add(new LinkInfo(link));
}
}
return links;
}
@SuppressWarnings("rawtypes")
private static LinkType LinkFromChild(Object child) {
if (child instanceof JAXBElement) {
return LinkFromElement((JAXBElement) child);
}
return null;
}
private static LinkType LinkFromElement(@SuppressWarnings("rawtypes") JAXBElement element) {
if (element.getDeclaredType() == LinkType.class) {
return (LinkType) element.getValue();
}
return null;
}
/**
* Is the given type inherited from ODataEntity
*
* @param type
* Type to check
* @return true if derived from ODataEntity
*/
public static boolean isODataEntityType(Class<?> type) {
return ODataEntity.class.isAssignableFrom(type);
}
/**
* Is the given type a collection of ODataEntity
*
* @param type
* Base type
* @param genericType
* Generic type
* @return true if it's List<OEntity> or derive from.
*/
public static boolean isODataEntityCollectionType(Class<?> type, Type genericType) {
if (ListResult.class != type) {
return false;
}
ParameterizedType pt = (ParameterizedType) genericType;
if (pt.getActualTypeArguments().length != 1) {
return false;
}
Class<?> typeClass = getCollectedType(genericType);
return isODataEntityType(typeClass);
}
/**
* Reflection helper to pull out the type parameter for
* a List<T>, where T is a ODataEntity<?> derived type.
*
* @param genericType
* type object for collection
* @return The class object for the type parameter.
*/
public static Class<?> getCollectedType(Type genericType) {
ParameterizedType pt = (ParameterizedType) genericType;
if (pt.getActualTypeArguments().length != 1) {
throw new IllegalArgumentException("genericType");
}
return (Class<?>) pt.getActualTypeArguments()[0];
}
}