Я пытался реализовать функцию надгробной плиты в моем приложении odata,
Я расширил ODataSingleProcessor и ODataServiceFactory , и у меня также есть ODataJPAServiceFactory , где a пытался использовать метод initializeODataJPAContext (все еще не работал ).
Мой вопрос на самом деле: где инициализировать readEntitySet метод OdataSingleProcessor ?
Я уже прочитал тонну учебников и до сих пор ничего не нашел.
ODataSingleProcessor
public class CustomODataSingleProcessor extends ODataSingleProcessor {
public CustomODataSingleProcessor(ODataContext oDataContext){
setContext(oDataContext);
}
CustomServiceFactory factory = new CustomServiceFactory();
TombstoneCallback tombstoneCallback;
@Override
public ODataResponse readEntitySet(final GetEntitySetUriInfo uriInfo, final String contentType)
throws ODataException {
tombstoneCallback = factory.getCallback(TombstoneCallback.class);
Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
callbacks.put(TombstoneCallback.CALLBACK_KEY_TOMBSTONE, tombstoneCallback);
URI uri = getContext().getPathInfo().getServiceRoot();
//tombstoneCallback.getTombstoneCallbackResult().getDeletedEntriesData();
EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(uri)
.callbacks(callbacks).build();
final ODataResponse response = new JsonEntityProvider()
.writeFeed(uriInfo.getTargetEntitySet(), getData(uriInfo), properties);
return response;
}
private List<Map<String, Object>> getData(GetEntitySetUriInfo uriInfo) throws EdmException {
List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
HashMap<String, Object> data = new HashMap<String, Object>();
for (String pName : uriInfo.getTargetEntitySet().getEntityType().getPropertyNames()) {
EdmProperty property = (EdmProperty) uriInfo.getTargetEntitySet().getEntityType().getProperty(pName);
Object mappedPropertyName = property.getMapping().getObject();
data.put(pName, mappedPropertyName);
values.add(data);
}
return values;
}
ODataServiceFactory
public class CustomODataServiceFactory extends ODataServiceFactory{
private ODataJPAContext oDataJPAContext;
private ODataContext oDataContext;
private boolean setDetailErrors = false;
private OnJPAWriteContent onJPAWriteContent = null;
private ODataJPATransaction oDataJPATransaction = null;
public CustomODataServiceFactory(ODataJPAContext oDataJPAContext) {
this.oDataJPAContext = oDataJPAContext;
}
@Override
public final ODataService createService(final ODataContext ctx) throws ODataException {
oDataContext = ctx;
validatePreConditions();
ODataJPAFactory factory = ODataJPAFactory.createFactory();
ODataJPAAccessFactory accessFactory = factory.getODataJPAAccessFactory();
// OData JPA Processor
if (oDataJPAContext.getODataContext() == null) {
oDataJPAContext.setODataContext(ctx);
}
//Processor Customizado
ODataSingleProcessor odataJPAProcessor = new CustomODataSingleProcessor(ctx);
// OData Entity Data Model Provider based on JPA
EdmProvider edmProvider = accessFactory.createJPAEdmProvider(oDataJPAContext);
return createODataSingleProcessorService(edmProvider, odataJPAProcessor);
}
public ODataSingleProcessor createCustomODataProcessor(ODataJPAContext oDataJPAContext) {
return null;
}
@Override
public ODataService createODataSingleProcessorService(EdmProvider provider, ODataSingleProcessor processor) {
return super.createODataSingleProcessorService(provider, processor);
}
/**
* @return an instance of type {@link ODataJPAContext}
* @throws ODataJPARuntimeException
*/
public final ODataJPAContext getODataJPAContext() throws ODataJPARuntimeException {
if (oDataJPAContext == null) {
oDataJPAContext = ODataJPAFactory.createFactory().getODataJPAAccessFactory().createODataJPAContext();
}
if (oDataContext != null) {
oDataJPAContext.setODataContext(oDataContext);
}
return oDataJPAContext;
}
@SuppressWarnings("unchecked")
@Override
public <T extends ODataCallback> T getCallback(final Class<T> callbackInterface) {
if (setDetailErrors == true) {
if (callbackInterface.isAssignableFrom(ODataErrorCallback.class)) {
return (T) new ODataJPAErrorCallback();
}
}
if (onJPAWriteContent != null) {
if (callbackInterface.isAssignableFrom(OnJPAWriteContent.class)) {
return (T) onJPAWriteContent;
}
}
if (oDataJPATransaction != null) {
if (callbackInterface.isAssignableFrom(ODataJPATransaction.class)) {
return (T) oDataJPATransaction;
}
}
return null;
}
/**
* The methods sets the context with a callback implementation for JPA provider specific content.
* For details refer to {@link org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent}
* @param onJPAWriteContent is an instance of type
* {@link org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent}
*/
protected void setOnWriteJPAContent(final OnJPAWriteContent onJPAWriteContent) {
this.onJPAWriteContent = onJPAWriteContent;
}
/**
* The methods sets the context with a callback implementation for JPA transaction specific content.
* For details refer to {@link ODataJPATransaction}
* @param oDataJPATransaction is an instance of type
* {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPATransaction}
*/
protected void setODataJPATransaction(final ODataJPATransaction oDataJPATransaction) {
this.oDataJPATransaction = oDataJPATransaction;
}
protected void setDetailErrors(final boolean setDetailErrors) {
this.setDetailErrors = setDetailErrors;
}
private void validatePreConditions() throws ODataJPARuntimeException {
if (oDataJPAContext.getEntityManager() == null) {
throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.ENTITY_MANAGER_NOT_INITIALIZED, null);
}
}
ODataJPAServiceFactory
public class CustomServiceFactory extends ODataJPAServiceFactory {
public final OnJPAWriteContent onDBWriteContent = new OnDBWriteContent();
@Override
public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
ODataJPAContext oDataJPAContext = getODataJPAContext();
oDataJPAContext.setEntityManagerFactory(JPAUtil.getEntityManagerFactory());
oDataJPAContext.setPersistenceUnitName(JPAEntityManagerFactory.PERSISTENCE_UNIT_NAME);
oDataJPAContext.setJPAEdmExtension(new DefaultEntityProcessingExtension());
setOnWriteJPAContent(onDBWriteContent);
return oDataJPAContext;
}
public <T extends ODataCallback> T getCallback(final Class<T> callbackInterface) {
T callback = null;
if (callbackInterface.isAssignableFrom(CustomDebugCallback.class)) {
callback = (T) new CustomDebugCallback();
} else {
callback = (T) super.getCallback(callbackInterface);
}
return callback;
}
@Override
public ODataService createODataSingleProcessorService(EdmProvider provider, ODataSingleProcessor processor) {
// TODO Auto-generated method stub
ODataSingleProcessorService service = (ODataSingleProcessorService) super.createODataSingleProcessorService(
provider, processor);
return service;
}
public class OnDBWriteContent implements OnJPAWriteContent {
@Override
public Blob getJPABlob(byte[] binaryData) throws ODataJPARuntimeException {
try {
return new JDBCBlob(binaryData);
} catch (SerialException e) {
ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
} catch (SQLException e) {
ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
}
return null;
}
@Override
public Clob getJPAClob(char[] characterData) throws ODataJPARuntimeException {
try {
return new JDBCClob(new String(characterData));
} catch (SQLException e) {
ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
}
return null;
}
}