summaryrefslogtreecommitdiffstats
path: root/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/SelectClause.java
blob: 259c6d12def0dee3f45fe17d4bd2343ce3334c78 (plain)
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
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.agfa.hap.sdo;

import java.util.Iterator;
import java.util.Map.Entry;

import com.agfa.hap.sdo.PropertyTree.MapBasedPropertyTree;
import commonj.sdo.Type;


/**
 * SnapshotDefinition defined by a comma-separated list of
 * property paths. Datatype properties (e.g. strings, dates etc) are included by default
 * and should not be part of the select clause. Many-valued properties that are not part of
 * the select string will be unavailable. Single-valued properties that are not part of
 * the select string will be available as proxies when they have their Identity property filled
 * in. Otherwise, they will not be part of the snapshot.
 * <p />
 * Using '*' as the name of a property indicates all properties of that type.
 * Using '**' indicates all properties recursively. This means all child
 * properties including all properties of those child properties (in case
 * they are not datatypes) recursively. This is similar to {@link ReachableDefinition}.
 * <p />
 * Ex. address, serviceRequests, medicalCase.* for the Patient type will return all
 * basic properties of Patient, its address, its serviceRequests. And also its complete
 * medicalCase including many-valued properties and all its direct links.
 * <p />
 * Properties that are part of the definition but are not available when creating the snapshot
 * will be accessed. That may result in {@link PropertyNotAvailableException} being thrown or
 * might result in lazy retrieval of data (e.g. when working with Hibernate proxies).  
 * @author AMOCZ
 */
public class SelectClause implements SnapshotDefinition {

	private PropertyTree propertyTree;
	
	protected SelectClause(PropertyTree propertyTree) {
		this.propertyTree = propertyTree;
	}
	
	public SelectClause(Type mainType) {
		this(mainType, "");
	}

	public SelectClause(Type mainType, String clause) {
		this.propertyTree = PropertyTree.newPropertyTree((com.agfa.hap.sdo.Type) mainType, clause);
	}
	
	@Override
	public boolean equals(Object other) {
		if (!(other instanceof SelectClause)) {
			return false;
		}
		return ((SelectClause) other).propertyTree == propertyTree;
	}
	
	@Override
	public int hashCode() {
		return propertyTree.hashCode();
	}
	
	public <T> void visit(DataMapper<T> mapper, ObjectPropertyVisitor visitor, T dataObject) throws Exception {
		visit(mapper, visitor, dataObject, propertyTree);
	}

	private <T> void visit(DataMapper<T> mapper, ObjectPropertyVisitor visitor, T dataObject, PropertyTree propertyTree) throws Exception {
		com.agfa.hap.sdo.Type type = mapper.getType(dataObject);
		if (type == null) {
			throw new IllegalArgumentException("Can't visit an object which doesn't have an sdo type. dataObject=" + dataObject);
		}
		visitor.startDataObject(dataObject, type);
		if (propertyTree != null) {
			for (Property property : type.getProperties()) {
				if (property.getType().isDataType()) {
					visitChild(mapper, visitor, dataObject, property, null);
				} else {
					PropertyTree tree = propertyTree.getProperty(property);
					if (tree == null) {
						visitProxy(mapper, visitor, dataObject, property);
					} else {
						visitChildObject(mapper, visitor, dataObject, property, tree);
					}
				}
			}
		}
		visitor.endDataObject(dataObject, type);
	}

	protected <T> void visitProxy(DataMapper<T> mapper, ObjectPropertyVisitor visitor, T dataObject, Property property) throws Exception {
		if (property.isMany()) {
			visitor.visitProxyProperty(dataObject, property, null);
		} else {
			Property idProp = property.getType().getIdentityProperty();
			if (idProp != null) {
				T child = (T) mapper.getProperty(dataObject, property);
				if (child != null) {
					Object identity = mapper.getProperty(child, idProp);
					if (identity != null) { // link doesn't have an identity, so no use to proxy
						visitor.visitProxyProperty(dataObject, property, identity);
					}
				}
			}
		}
	}

	protected <T> void visitChildObject(DataMapper<T> mapper, ObjectPropertyVisitor visitor, T dataObject, Property property, PropertyTree tree) throws Exception {
		if (mapper.isBulkProperty(dataObject.getClass(), property)) {
			visitor.visitBulkProperty(dataObject, property, new SelectClause(tree));
			return;
		} 
		if (property.isMany()) {
			Iterator<? extends T> it = mapper.getObjects(dataObject, property);
			while (it.hasNext()) {
				visitChild(mapper, visitor, dataObject, property, it.next(), tree);
			}
		} else {
			visitChild(mapper, visitor, dataObject, property, tree);
		}
	}

	protected <T> void visitChild(DataMapper<T> mapper, ObjectPropertyVisitor visitor, T parent, Property property,  PropertyTree propertyTree) throws Exception {
		if (mapper.isBulkProperty(parent.getClass(), property)) {
			visitor.visitBulkProperty(parent, property, new SelectClause(propertyTree));
			return;
		} 
		Object value = mapper.getProperty(parent, property);
		visitChild(mapper, visitor, parent, property, value, propertyTree);
	}

	private <T> void visitChild(DataMapper<T> mapper, ObjectPropertyVisitor visitor, T parent, Property property, Object value, PropertyTree propertyTree) throws Exception {
		if (value != null) {
			boolean recurse = visitor.visitProperty(parent, property, value);
			if (recurse) {
				visit(mapper, visitor, (T) value, propertyTree);
			}
		}
	}

	public PropertyTree getPropertyTree() {
		return propertyTree;		
	}		
	
	public String asCommaSeparatedString(){
		return this.getPropertyTree().asCommmaSeparatedString();
	}
	
}