/**
*
* 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.
*
* 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}.
*
* 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.
*
* 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 void visit(DataMapper mapper, ObjectPropertyVisitor visitor, T dataObject) throws Exception {
visit(mapper, visitor, dataObject, propertyTree);
}
private void visit(DataMapper 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 void visitProxy(DataMapper 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 void visitChildObject(DataMapper 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 void visitChild(DataMapper 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 void visitChild(DataMapper 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();
}
}