}
}
@SuppressWarnings("rawtypes")
protected Response create(String version, String persistenceUnit, String type, HttpHeaders hh, UriInfo uriInfo, URI baseURI, InputStream in) throws JAXBException {
PersistenceContext app = getPersistenceContext(persistenceUnit, baseURI, version, null);
if (app == null) {
JPARSLogger.fine("jpars_could_not_find_persistence_context", new Object[] { persistenceUnit });
return Response.status(Status.NOT_FOUND).type(StreamingOutputMarshaller.getResponseMediaType(hh)).build();
}
ClassDescriptor descriptor = app.getDescriptor(type);
if (descriptor == null) {
JPARSLogger.fine("jpars_could_not_find_class_in_persistence_unit", new Object[] { type, persistenceUnit });
return Response.status(Status.NOT_FOUND).type(StreamingOutputMarshaller.getResponseMediaType(hh)).build();
}
Object entity = null;
try {
entity = app.unmarshalEntity(type, mediaType(hh.getAcceptableMediaTypes()), in);
} catch (JAXBException e) {
JPARSLogger.fine("exception_while_unmarhalling_entity", new Object[] { type, persistenceUnit, e.toString() });
return Response.status(Status.BAD_REQUEST).type(StreamingOutputMarshaller.getResponseMediaType(hh)).build();
}
// maintain idempotence on PUT by disallowing sequencing
AbstractDirectMapping sequenceMapping = descriptor.getObjectBuilder().getSequenceMapping();
if (sequenceMapping != null) {
Object value = sequenceMapping.getAttributeAccessor().getAttributeValueFromObject(entity);
if (descriptor.getObjectBuilder().isPrimaryKeyComponentInvalid(value, descriptor.getPrimaryKeyFields().indexOf(descriptor.getSequenceNumberField()))
|| descriptor.getSequence().shouldAlwaysOverrideExistingValue()) {
JPARSLogger.fine("jpars_put_not_idempotent", new Object[] { type, persistenceUnit });
return Response.status(Status.BAD_REQUEST).type(StreamingOutputMarshaller.getResponseMediaType(hh)).build();
}
}
// maintain idempotence on PUT by disallowing sequencing in relationships
List<DatabaseMapping> mappings = descriptor.getMappings();
if ((mappings != null) && (!mappings.isEmpty())) {
for (DatabaseMapping mapping : mappings) {
if (mapping instanceof ForeignReferenceMapping) {
ForeignReferenceMapping fkMapping = (ForeignReferenceMapping) mapping;
if ((fkMapping.isCascadePersist()) || (fkMapping.isCascadeMerge())) {
ClassDescriptor referenceDescriptor = fkMapping.getReferenceDescriptor();
if (referenceDescriptor != null) {
if (referenceDescriptor instanceof RelationalDescriptor) {
RelationalDescriptor relDesc = (RelationalDescriptor) referenceDescriptor;
AbstractDirectMapping relSequenceMapping = relDesc.getObjectBuilder().getSequenceMapping();
if (relSequenceMapping != null) {
Object value = mapping.getAttributeAccessor().getAttributeValueFromObject(entity);
if (value != null) {
if (value instanceof ValueHolder) {
ValueHolder holder = (ValueHolder) value;
if (holder != null) {
Object obj = holder.getValue();
if (obj != null) {
JPARSLogger.fine("jpars_put_not_idempotent", new Object[] { type, persistenceUnit });
return Response.status(Status.BAD_REQUEST).build();
}
}
} else if (value instanceof Collection) {
if (!(((Collection) value).isEmpty())) {
JPARSLogger.fine("jpars_put_not_idempotent", new Object[] { type, persistenceUnit });
return Response.status(Status.BAD_REQUEST).build();
}
}
}
}
}
}
}
}
}
}
// No sequencing in relationships, we can create the object now...
app.create(getMatrixParameters(uriInfo, persistenceUnit), entity);
ResponseBuilder rb = Response.status(Status.CREATED);
rb.entity(new StreamingOutputMarshaller(app, entity, hh.getAcceptableMediaTypes()));
return rb.build();
}