Search in sources :

Example 21 with StringApiResponse

use of org.entando.entando.aps.system.services.api.model.StringApiResponse in project entando-core by entando.

the class ApiContentInterface method deleteContent.

public StringApiResponse deleteContent(Properties properties) throws Throwable {
    StringApiResponse response = new StringApiResponse();
    try {
        String id = properties.getProperty("id");
        Content masterContent = this.getContentManager().loadContent(id, false);
        if (null == masterContent) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content with code '" + id + "' does not exist", Response.Status.CONFLICT);
        }
        UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
        if (null == user) {
            user = this.getUserManager().getGuestUser();
        }
        if (!this.getContentAuthorizationHelper().isAuth(user, masterContent)) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content groups makes the new content not allowed for user " + user.getUsername(), Response.Status.FORBIDDEN);
        }
        List<String> references = ((ContentUtilizer) this.getContentManager()).getContentUtilizers(id);
        if (references != null && references.size() > 0) {
            boolean found = false;
            for (int i = 0; i < references.size(); i++) {
                String reference = references.get(i);
                ContentRecordVO record = this.getContentManager().loadContentVO(reference);
                if (null != record) {
                    found = true;
                    response.addError(new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, "Content " + id + " referenced to content " + record.getId() + " - '" + record.getDescr() + "'", Response.Status.CONFLICT));
                }
            }
            if (found) {
                response.setResult(IResponseBuilder.FAILURE, null);
                return response;
            }
        }
        if (masterContent.isOnLine()) {
            this.getContentManager().removeOnLineContent(masterContent);
        }
        String removeWorkVersionString = properties.getProperty("removeWorkVersion");
        boolean removeWorkVersion = (null != removeWorkVersionString) ? Boolean.parseBoolean(removeWorkVersionString) : false;
        if (removeWorkVersion) {
            this.getContentManager().deleteContent(masterContent);
        }
        response.setResult(IResponseBuilder.SUCCESS, null);
    } catch (ApiException ae) {
        response.addErrors(ae.getErrors());
        response.setResult(IResponseBuilder.FAILURE, null);
    } catch (Throwable t) {
        _logger.error("Error deleting content", t);
        throw new ApsSystemException("Error deleting content", t);
    }
    return response;
}
Also used : ContentRecordVO(com.agiletec.plugins.jacms.aps.system.services.content.model.ContentRecordVO) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ContentUtilizer(com.agiletec.plugins.jacms.aps.system.services.content.ContentUtilizer) UserDetails(com.agiletec.aps.system.services.user.UserDetails) Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent) ApiError(org.entando.entando.aps.system.services.api.model.ApiError) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 22 with StringApiResponse

use of org.entando.entando.aps.system.services.api.model.StringApiResponse in project entando-core by entando.

the class ApiResourceInterface method deleteResource.

private StringApiResponse deleteResource(Properties properties, ResourceInterface resource) throws ApiException, Throwable {
    StringApiResponse response = new StringApiResponse();
    try {
        String id = properties.getProperty("id");
        if (null == resource) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Resource with code '" + id + "' does not exist", Response.Status.CONFLICT);
        }
        UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
        if (null == user) {
            user = this.getUserManager().getGuestUser();
        }
        if (!this.getAuthorizationManager().isAuthOnGroup(user, resource.getMainGroup())) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Resource not allowed for user '" + user.getUsername() + "' - resource group '" + resource.getMainGroup() + "'", Response.Status.FORBIDDEN);
        }
        List<String> references = ((ResourceUtilizer) this.getContentManager()).getResourceUtilizers(id);
        if (references != null && references.size() > 0) {
            boolean found = false;
            for (int i = 0; i < references.size(); i++) {
                String reference = references.get(i);
                ContentRecordVO record = this.getContentManager().loadContentVO(reference);
                if (null != record) {
                    found = true;
                    response.addError(new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, "Resource " + id + " referenced to content " + record.getId() + " - '" + record.getDescr() + "'", Response.Status.CONFLICT));
                }
            }
            if (found) {
                response.setResult(IResponseBuilder.FAILURE, null);
                return response;
            }
        }
        this.getResourceManager().deleteResource(resource);
        response.setResult(IResponseBuilder.SUCCESS, null);
    } catch (ApiException ae) {
        response.addErrors(ae.getErrors());
        response.setResult(IResponseBuilder.FAILURE, null);
    } catch (Throwable t) {
        _logger.error("Error deleting resource", t);
        // ApsSystemUtils.logThrowable(t, this, "deleteResource");
        throw new ApsSystemException("Error deleting resource", t);
    }
    return response;
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ContentRecordVO(com.agiletec.plugins.jacms.aps.system.services.content.model.ContentRecordVO) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException) ResourceUtilizer(com.agiletec.plugins.jacms.aps.system.services.resource.ResourceUtilizer)

Example 23 with StringApiResponse

use of org.entando.entando.aps.system.services.api.model.StringApiResponse in project entando-core by entando.

the class ApiResourceInterface method deleteResource.

/**
 * Delete a Resource. The method can be called by Entando API Engine.
 * @param properties The properties of the DELETE method call.
 * @throws Throwable Il case of error.
 */
public StringApiResponse deleteResource(Properties properties) throws Throwable {
    StringApiResponse response = null;
    try {
        String id = properties.getProperty("id");
        ResourceInterface resource = this.getResourceManager().loadResource(id);
        response = this.deleteResource(properties, resource);
    } catch (Throwable t) {
        _logger.error("Error deleting resource", t);
        // ApsSystemUtils.logThrowable(t, this, "deleteResource");
        throw new ApsSystemException("Error deleting resource", t);
    }
    return response;
}
Also used : ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ResourceInterface(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse)

Example 24 with StringApiResponse

use of org.entando.entando.aps.system.services.api.model.StringApiResponse in project entando-core by entando.

the class ApiResourceInterface method deleteResource.

private StringApiResponse deleteResource(Properties properties, String expectedTypeCode) throws ApiException, Throwable {
    StringApiResponse response = null;
    try {
        String id = properties.getProperty("id");
        ResourceInterface resource = this.getResourceManager().loadResource(id);
        if (null != resource && !resource.getType().equals(expectedTypeCode)) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid resource type - '" + resource.getType() + "'", Response.Status.CONFLICT);
        }
        properties.setProperty(RESOURCE_TYPE_CODE_PARAM, expectedTypeCode);
        response = this.deleteResource(properties, resource);
    } catch (ApiException ae) {
        throw ae;
    } catch (Throwable t) {
        _logger.error("Error deleting resource", t);
        // ApsSystemUtils.logThrowable(t, this, "deleteResource");
        throw new ApsSystemException("Error deleting resource", t);
    }
    return response;
}
Also used : ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ResourceInterface(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 25 with StringApiResponse

use of org.entando.entando.aps.system.services.api.model.StringApiResponse in project entando-core by entando.

the class TestApiI18nLabelInterface method testCreateNewLabel.

protected void testCreateNewLabel(MediaType mediaType) throws Throwable {
    String key = "TEST_LABEL_KEY";
    String label = this._i18nManager.getLabel(key, "it");
    assertNull(label);
    ApsProperties labels = new ApsProperties();
    labels.put("en", "Test label");
    labels.put("it", "Label di Test");
    JAXBI18nLabel jaxbLabel = new JAXBI18nLabel(key, labels);
    ApiResource labelResource = this.getApiCatalogManager().getResource("core", "i18nlabel");
    ApiMethod postMethod = labelResource.getPostMethod();
    Properties properties = super.createApiProperties("admin", "it", mediaType);
    try {
        Object response = this.getResponseBuilder().createResponse(postMethod, jaxbLabel, properties);
        assertNotNull(response);
        assertTrue(response instanceof StringApiResponse);
        assertEquals(IResponseBuilder.SUCCESS, ((StringApiResponse) response).getResult());
        label = this._i18nManager.getLabel(key, "it");
        assertEquals("Label di Test", label);
    } catch (Exception e) {
        throw e;
    } finally {
        this._i18nManager.deleteLabelGroup(key);
    }
}
Also used : ApiResource(org.entando.entando.aps.system.services.api.model.ApiResource) JAXBI18nLabel(org.entando.entando.aps.system.services.i18n.model.JAXBI18nLabel) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) Properties(java.util.Properties) ApsProperties(com.agiletec.aps.util.ApsProperties) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApsProperties(com.agiletec.aps.util.ApsProperties)

Aggregations

StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)28 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)23 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)20 UserDetails (com.agiletec.aps.system.services.user.UserDetails)9 ApiError (org.entando.entando.aps.system.services.api.model.ApiError)8 IApsEntity (com.agiletec.aps.system.common.entity.model.IApsEntity)6 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)5 IEntityTypesConfigurer (com.agiletec.aps.system.common.entity.IEntityTypesConfigurer)4 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)4 ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)4 JAXBContent (org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent)4 JAXBDataObject (org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject)3 DataObject (org.entando.entando.aps.system.services.dataobject.model.DataObject)3 IUserProfile (org.entando.entando.aps.system.services.userprofile.model.IUserProfile)3 ContentRecordVO (com.agiletec.plugins.jacms.aps.system.services.content.model.ContentRecordVO)2 BaseResourceDataBean (com.agiletec.plugins.jacms.aps.system.services.resource.model.BaseResourceDataBean)2 ResourceInterface (com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface)2 Properties (java.util.Properties)2 ApiResource (org.entando.entando.aps.system.services.api.model.ApiResource)2 IEntityManager (com.agiletec.aps.system.common.entity.IEntityManager)1