Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*****************************************************************
* 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 org.apache.cayenne;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.apache.cayenne.map.DbEntity;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.query.ObjectIdQuery;
import org.apache.cayenne.query.Query;
import org.apache.cayenne.reflect.ClassDescriptor;
import org.apache.cayenne.reflect.Property;
import org.apache.cayenne.reflect.PropertyUtils;
/**
* Various utils for processing persistent objects and their properties
*
* DataObjects and Primary Keys: All methods that allow to extract primary key values
* or use primary keys to find objects are provided for convenience. Still the author's
* belief is that integer sequential primary keys are meaningless in the object model and
* are pure database artifacts. Therefore relying heavily on direct access to PK provided
* via this class (or other such Cayenne API) is not a clean design practice in many
* cases, and sometimes may actually lead to security issues.
*
*
* @since 3.1 its predecessor was called DataObjectUtils
*/
public class Cayenne {
/**
* A special property denoting a size of the to-many collection, when encountered at
* the end of the path
*/
final static String PROPERTY_COLLECTION_SIZE = "@size";
/**
* Returns mapped ObjEntity for object. If an object is transient or is not mapped
* returns null.
*/
public static ObjEntity getObjEntity(Persistent p) {
return (p.getObjectContext() != null) ? p
.getObjectContext()
.getEntityResolver()
.lookupObjEntity(p) : null;
}
/**
* Returns class descriptor for the object or null if the object is not registered
* with an ObjectContext or descriptor was not found.
*/
public static ClassDescriptor getClassDescriptor(Persistent object) {
ObjectContext context = object.getObjectContext();
if (context == null) {
return null;
}
return context.getEntityResolver().getClassDescriptor(
object.getObjectId().getEntityName());
}
/**
* Returns property descriptor for specified property.
*
* @param properyName path to the property
* @return property descriptor, null if not found
*/
public static Property getProperty(Persistent object, String properyName) {
ClassDescriptor descriptor = getClassDescriptor(object);
if (descriptor == null) {
return null;
}
return descriptor.getProperty(properyName);
}
/**
* Returns a value of the property identified by a property path. Supports reading
* both mapped and unmapped properties. Unmapped properties are accessed in a manner
* consistent with JavaBeans specification.
*
* Property path (or nested property) is a dot-separated path used to traverse object
* relationships until the final object is found. If a null object found while
* traversing path, null is returned. If a list is encountered in the middle of the
* path, CayenneRuntimeException is thrown. Unlike
* {@link #readPropertyDirectly(String)}, this method will resolve an object if it is
* HOLLOW.
*
* Examples:
*
*
*
Read this object property:
* String name = (String)Cayenne.readNestedProperty(artist, "name");
*
*
*
Read an object related to this object:
* Gallery g = (Gallery)Cayenne.readNestedProperty(paintingInfo, "toPainting.toGallery");
*
*
*
*
Read a property of an object related to this object:
* String name = (String)Cayenne.readNestedProperty(painting, "toArtist.artistName");
*
*
*
Read to-many relationship in the middle of the path:
* List names = (List)Cayenne.readNestedProperty(artist, "paintingArray.paintingName");
*
*
*
*
*/
public static Object readNestedProperty(Object o, String path) {
if (o == null) {
return null;
}
else if (o instanceof DataObject) {
return ((DataObject) o).readNestedProperty(path);
}
else if (o instanceof Collection>) {
// This allows people to put @size at the end of a property
// path and be able to find out the size of a relationship.
Collection> collection = (Collection>) o;
if (path.equals(PROPERTY_COLLECTION_SIZE)) {
return collection.size();
}
// Support for collection property in the middle of the path
Collection