protected Response findAttributeInternal(String version, String persistenceUnit, String type, String id, String attribute, HttpHeaders headers, UriInfo uriInfo) {
JPARSLogger.entering(CLASS_NAME, "findAttributeInternal", new Object[] { "GET", version, persistenceUnit, type, id, attribute, uriInfo.getRequestUri().toASCIIString() });
EntityManager em = null;
try {
PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
Object entityId = IdHelper.buildId(context, type, id);
em = context.getEmf().createEntityManager(getMatrixParameters(uriInfo, persistenceUnit));
Object entity = null;
entity = em.find(context.getClass(type), entityId, getQueryParameters(uriInfo));
DatabaseSession serverSession = context.getServerSession();
ClassDescriptor descriptor = serverSession.getClassDescriptor(context.getClass(type));
if (descriptor == null) {
throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(type, persistenceUnit);
}
DatabaseMapping attributeMapping = descriptor.getMappingForAttributeName(attribute);
if ((attributeMapping == null) || (entity == null)) {
throw JPARSException.databaseMappingCouldNotBeFoundForEntityAttribute(attribute, type, id, persistenceUnit);
}
Object result = null;
if (!attributeMapping.isCollectionMapping()) {
result = attributeMapping.getRealAttributeValueFromAttribute(attributeMapping.getAttributeValueFromObject(entity), entity, (AbstractSession) serverSession);
if (result == null) {
JPARSLogger.error("jpars_could_not_find_entity_for_attribute", new Object[] { attribute, type, id, persistenceUnit });
throw JPARSException.attributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
}
return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, headers, uriInfo, context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING));
}
ReadQuery query = (ReadQuery) ((((ForeignReferenceMapping) attributeMapping).getSelectionQuery()).clone());
if (query == null) {
throw JPARSException.selectionQueryForAttributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
}
FeatureSet featureSet = context.getSupportedFeatureSet();
AbstractSession clientSession = context.getClientSession(em);
if (featureSet.isSupported(Feature.PAGING)) {
FeatureRequestValidator requestValidator = featureSet.getRequestValidator(Feature.PAGING);
Map<String, Object> map = new HashMap<String, Object>();
map.put(PagingRequestValidator.DB_QUERY, query);
if (requestValidator.isRequested(uriInfo, null)) {
if (!requestValidator.isRequestValid(uriInfo, map)) {
throw JPARSException.invalidPagingRequest();
}
// check orderBy, and generate a warning if there is none
checkOrderBy(query);
result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, headers, uriInfo, context.getSupportedFeatureSet().getResponseBuilder(Feature.PAGING));
}
}
result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, headers, uriInfo, context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING));
} catch (Exception ex) {
throw JPARSException.exceptionOccurred(ex);
} finally {
if (em != null) {
if (em.isOpen()) {