use of org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject in project entando-core by entando.
the class ApiDataObjectInterface method updateDataObject.
public StringApiResponse updateDataObject(JAXBDataObject jaxbDataObject, Properties properties) throws Throwable {
StringApiResponse response = new StringApiResponse();
try {
String typeCode = jaxbDataObject.getTypeCode();
DataObject prototype = (DataObject) this.getDataObjectManager().getEntityPrototype(typeCode);
if (null == prototype) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject type with code '" + typeCode + "' does not exist", Response.Status.CONFLICT);
}
DataObject content = (DataObject) jaxbDataObject.buildEntity(prototype, this.getCategoryManager(), null);
DataObject masterData = this.getDataObjectManager().loadDataObject(content.getId(), false);
if (null == masterData) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject with code '" + content.getId() + "' does not exist", Response.Status.CONFLICT);
} else if (!masterData.getMainGroup().equals(content.getMainGroup())) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid main group " + content.getMainGroup() + " not equals then master " + masterData.getMainGroup(), Response.Status.CONFLICT);
}
response = this.validateAndSaveDataObject(content, properties);
} catch (ApiException ae) {
response.addErrors(ae.getErrors());
response.setResult(IResponseBuilder.FAILURE, null);
} catch (Throwable t) {
_logger.error("Error updating DataObject", t);
throw new ApsSystemException("Error updating DataObject", t);
}
return response;
}
use of org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject in project entando-core by entando.
the class ApiDataObjectInterface method addDataObject.
public StringApiResponse addDataObject(JAXBDataObject jaxbDataObject, Properties properties) throws Throwable {
StringApiResponse response = new StringApiResponse();
try {
String typeCode = jaxbDataObject.getTypeCode();
DataObject prototype = (DataObject) this.getDataObjectManager().getEntityPrototype(typeCode);
if (null == prototype) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject type with code '" + typeCode + "' does not exist", Response.Status.CONFLICT);
}
DataObject dataObject = (DataObject) jaxbDataObject.buildEntity(prototype, this.getCategoryManager(), null);
if (null != dataObject.getId()) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "You cannot specify DataObject Id", Response.Status.CONFLICT);
}
UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
dataObject.setFirstEditor((null != user) ? user.getUsername() : SystemConstants.GUEST_USER_NAME);
response = this.validateAndSaveDataObject(dataObject, properties);
} catch (ApiException ae) {
response.addErrors(ae.getErrors());
response.setResult(IResponseBuilder.FAILURE, null);
} catch (Throwable t) {
_logger.error("Error adding DataObject", t);
throw new ApsSystemException("Error adding DataObject", t);
}
return response;
}
use of org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject in project entando-core by entando.
the class ApiDataObjectInterface method getDataObject.
public JAXBDataObject getDataObject(Properties properties) throws ApiException, Throwable {
JAXBDataObject jaxbDataObject = null;
String id = properties.getProperty("id");
try {
String langCode = properties.getProperty(SystemConstants.API_LANG_CODE_PARAMETER);
DataObject mainDataObject = this.getPublicDataObject(id);
jaxbDataObject = this.getJAXBDataObjectInstance(mainDataObject, langCode);
UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
if (null == user) {
user = this.getUserManager().getGuestUser();
}
if (!this.getDataObjectAuthorizationHelper().isAuth(user, mainDataObject)) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Required DataObject '" + id + "' does not allowed", Response.Status.FORBIDDEN);
}
} catch (ApiException ae) {
throw ae;
} catch (Throwable t) {
_logger.error("error in getDataObject", t);
throw new ApsSystemException("Error into API method", t);
}
return jaxbDataObject;
}
use of org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject in project entando-core by entando.
the class TestApiDataObjectInterface method testCreateNewDataObject.
protected void testCreateNewDataObject(MediaType mediaType, String dataObjectId) throws Throwable {
String dateNow = DateConverter.getFormattedDate(new Date(), SystemConstants.DATA_TYPE_METADATA_DATE_FORMAT);
EntitySearchFilter filter = new EntitySearchFilter(IDataObjectManager.DATA_OBJECT_CREATION_DATE_FILTER_KEY, false, dateNow, null);
EntitySearchFilter[] filters = { filter };
List<String> ids = this.dataObjectManager.searchId(filters);
assertTrue(ids.isEmpty());
JAXBDataObject jaxbDataObject = this.testGetDataObject(mediaType, "admin", dataObjectId, "it");
ApiResource dataTypeResource = this.getApiCatalogManager().getResource("core", "dataObject");
ApiMethod postMethod = dataTypeResource.getPostMethod();
Properties properties = super.createApiProperties("admin", "it", mediaType);
try {
jaxbDataObject.setId(null);
Object response = this.getResponseBuilder().createResponse(postMethod, jaxbDataObject, properties);
assertNotNull(response);
assertTrue(response instanceof StringApiResponse);
assertEquals(IResponseBuilder.SUCCESS, ((StringApiResponse) response).getResult());
ids = this.dataObjectManager.searchId(filters);
assertEquals(1, ids.size());
String newDataObjectId = ids.get(0);
DataObject newDataType = this.dataObjectManager.loadDataObject(newDataObjectId, false);
DataObject masterDataType = this.dataObjectManager.loadDataObject(dataObjectId, true);
List<AttributeInterface> attributes = masterDataType.getAttributeList();
for (int i = 0; i < attributes.size(); i++) {
AttributeInterface attribute = attributes.get(i);
AttributeInterface newAttribute = (AttributeInterface) newDataType.getAttribute(attribute.getName());
this.checkAttributes(attribute, newAttribute);
}
} catch (Exception e) {
throw e;
} finally {
ids = this.dataObjectManager.searchId(filters);
if (!ids.isEmpty()) {
for (int i = 0; i < ids.size(); i++) {
String id = ids.get(i);
DataObject dataObject = this.dataObjectManager.loadDataObject(id, false);
this.dataObjectManager.deleteDataObject(dataObject);
}
}
}
}
use of org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject in project entando-core by entando.
the class TestApiDataObjectInterface method testGetDataObject.
protected JAXBDataObject testGetDataObject(MediaType mediaType, String username, String dataId, String langCode) throws Throwable {
ApiResource dataResource = this.getApiCatalogManager().getResource("core", "dataObject");
ApiMethod getMethod = dataResource.getGetMethod();
Properties properties = super.createApiProperties(username, langCode, mediaType);
properties.put("id", dataId);
Object result = this.getResponseBuilder().createResponse(getMethod, properties);
assertNotNull(result);
ApiDataObjectInterface apiDataObjectInterface = (ApiDataObjectInterface) this.getApplicationContext().getBean("ApiDataObjectInterface");
Object singleResult = apiDataObjectInterface.getDataObject(properties);
assertNotNull(singleResult);
String toString = this.marshall(singleResult, mediaType);
InputStream stream = new ByteArrayInputStream(toString.getBytes());
JAXBDataObject jaxbData = (JAXBDataObject) UnmarshalUtils.unmarshal(super.getApplicationContext(), JAXBDataObject.class, stream, mediaType);
assertNotNull(jaxbData);
return jaxbData;
}
Aggregations