Search in sources :

Example 1 with JAXBDataObject

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;
}
Also used : JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 2 with JAXBDataObject

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;
}
Also used : JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) UserDetails(com.agiletec.aps.system.services.user.UserDetails) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 3 with JAXBDataObject

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;
}
Also used : JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) UserDetails(com.agiletec.aps.system.services.user.UserDetails) JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 4 with 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);
            }
        }
    }
}
Also used : ApiResource(org.entando.entando.aps.system.services.api.model.ApiResource) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) Properties(java.util.Properties) Date(java.util.Date) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) EntitySearchFilter(com.agiletec.aps.system.common.entity.model.EntitySearchFilter) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) AttributeInterface(com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)

Example 5 with JAXBDataObject

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;
}
Also used : ApiResource(org.entando.entando.aps.system.services.api.model.ApiResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) Properties(java.util.Properties)

Aggregations

JAXBDataObject (org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject)5 DataObject (org.entando.entando.aps.system.services.dataobject.model.DataObject)5 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)3 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)3 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)3 UserDetails (com.agiletec.aps.system.services.user.UserDetails)2 Properties (java.util.Properties)2 ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)2 ApiResource (org.entando.entando.aps.system.services.api.model.ApiResource)2 EntitySearchFilter (com.agiletec.aps.system.common.entity.model.EntitySearchFilter)1 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1