@Override public void setPredefinedValidationStampImage(ID predefinedValidationStampId, Document document) { getNamedParameterJdbcTemplate().update( "UPDATE PREDEFINED_VALIDATION_STAMPS SET IMAGETYPE = :type, IMAGEBYTES = :content WHERE ID = :id", params("id", predefinedValidationStampId.getValue()) .addValue("type", Document.isValid(document) ? document.getType() : null) .addValue("content", Document.isValid(document) ? document.getContent() : null) ); }
protected Document toDocument(ResultSet rs) throws SQLException { String type = rs.getString("imagetype"); byte[] bytes = rs.getBytes("imagebytes"); if (StringUtils.isNotBlank(type) && bytes != null && bytes.length > 0) { return new Document(type, bytes); } else { return null; } }
public static boolean isValid(Document document) { return document != null && !document.isEmpty(); } }
public static void checkImage(Document document) { // Checks the image type if (document != null && !ArrayUtils.contains(ACCEPTED_IMAGE_TYPES, document.getType())) { throw new ImageTypeNotAcceptedException(document.getType(), ACCEPTED_IMAGE_TYPES); } // Checks the image length int size = document != null ? document.getContent().length : 0; if (size > ICON_IMAGE_SIZE_MAX) { throw new ImageFileSizeException(size, ICON_IMAGE_SIZE_MAX); } } }
private void copyValidationStampContent(ValidationStamp sourceValidationStamp, ValidationStamp targetValidationStamp) { // Copy of the image Document image = structureService.getValidationStampImage(sourceValidationStamp.getId()); if (Document.isValid(image)) { structureService.setValidationStampImage(targetValidationStamp.getId(), image); } // Copy of properties doCopyProperties(sourceValidationStamp, targetValidationStamp, replacementFn, syncPolicy); } }
@Override public <T> T upload(ResponseParser<T> responseParser, String name, Document document, String fileName, String path, Object... parameters) { HttpPost post = new HttpPost(getUrl(path)); // Sets the content post.setEntity( MultipartEntityBuilder.create() .addBinaryBody( name, document.getContent(), ContentType.parse(document.getType()), fileName ) .build() ); // OK return request(post, responseParser); }
private void copyPromotionLevelContent(PromotionLevel sourcePromotionLevel, PromotionLevel targetPromotionLevel) { // Copy of the image Document image = structureService.getPromotionLevelImage(sourcePromotionLevel.getId()); if (Document.isValid(image)) { structureService.setPromotionLevelImage(targetPromotionLevel.getId(), image); } // Copy of properties doCopyProperties(sourcePromotionLevel, targetPromotionLevel, replacementFn, syncPolicy); } }
@Override public void setPredefinedPromotionLevelImage(ID predefinedPromotionLevelId, Document document) { getNamedParameterJdbcTemplate().update( "UPDATE PREDEFINED_PROMOTION_LEVELS SET IMAGETYPE = :type, IMAGEBYTES = :content WHERE ID = :id", params("id", predefinedPromotionLevelId.getValue()) .addValue("type", Document.isValid(document) ? document.getType() : null) .addValue("content", Document.isValid(document) ? document.getContent() : null) ); }
@Override public <T> T upload(ResponseParser<T> responseParser, String name, Document document, String fileName, String path, Object... parameters) { HttpPost post = new HttpPost(getUrl(path)); // Sets the content post.setEntity( MultipartEntityBuilder.create() .addBinaryBody( name, document.getContent(), ContentType.parse(document.getType()), fileName ) .build() ); // OK return request(post, responseParser); }
@Override public Document download(String path, Object... parameters) { HttpGet get = new HttpGet(getUrl(path)); return request(get, (request, response, entity) -> { return handleErrorCode(request, response, () -> { // Gets the content as bytes byte[] bytes; try { bytes = EntityUtils.toByteArray(entity); } catch (IOException e) { throw new ClientIOException(request, e); } if (bytes == null || bytes.length == 0) { return Document.EMPTY; } // OK return new Document( entity.getContentType().getValue(), bytes ); }); }); }
@Override public void setValidationStampImage(ID validationStampId, Document document) { getNamedParameterJdbcTemplate().update( "UPDATE VALIDATION_STAMPS SET IMAGETYPE = :type, IMAGEBYTES = :content WHERE ID = :id", params("id", validationStampId.getValue()) .addValue("type", Document.isValid(document) ? document.getType() : null) .addValue("content", Document.isValid(document) ? document.getContent() : null) ); }
@Override public void setPromotionLevelImage(ID promotionLevelId, Document document) { getNamedParameterJdbcTemplate().update( "UPDATE PROMOTION_LEVELS SET IMAGETYPE = :type, IMAGEBYTES = :content WHERE ID = :id", params("id", promotionLevelId.getValue()) .addValue("type", document != null ? document.getType() : null) .addValue("content", document != null ? document.getContent() : null) ); }
@Override public Document download(String path, Object... parameters) { HttpGet get = new HttpGet(getUrl(path)); return request(get, (request, response, entity) -> { return handleErrorCode(request, response, () -> { // Gets the content as bytes byte[] bytes; try { bytes = EntityUtils.toByteArray(entity); } catch (IOException e) { throw new ClientIOException(request, e); } if (bytes == null || bytes.length == 0) { return Document.EMPTY; } // OK return new Document( entity.getContentType().getValue(), bytes ); }); }); }
@Override public void bulkUpdatePromotionLevels(ID promotionLevelId) { // Description & name PromotionLevel promotionLevel = getPromotionLevel(promotionLevelId); String description = promotionLevel.getDescription(); String name = promotionLevel.getName(); // Image Document image = getPromotionLevelImage(promotionLevelId); // Bulk update getNamedParameterJdbcTemplate().update( "UPDATE PROMOTION_LEVELS SET IMAGETYPE = :type, IMAGEBYTES = :content, DESCRIPTION = :description " + "WHERE ID <> :id AND NAME = :name", params("id", promotionLevelId.getValue()) .addValue("name", name) .addValue("description", description) .addValue("type", Document.isValid(image) ? image.getType() : null) .addValue("content", Document.isValid(image) ? image.getContent() : null) ); }
@Override public void bulkUpdateValidationStamps(ID validationStampId) { // Description & name ValidationStamp validationStamp = getValidationStamp(validationStampId); String description = validationStamp.getDescription(); String name = validationStamp.getName(); // Image Document image = getValidationStampImage(validationStampId); // Bulk update getNamedParameterJdbcTemplate().update( "UPDATE VALIDATION_STAMPS SET IMAGETYPE = :type, IMAGEBYTES = :content, DESCRIPTION = :description, " + "DATA_TYPE_ID = :dataTypeId, DATA_TYPE_CONFIG = CAST(:dataTypeConfig AS JSONB) " + "WHERE ID <> :id AND NAME = :name", params("id", validationStampId.getValue()) .addValue("name", name) .addValue("description", description) .addValue("type", Document.isValid(image) ? image.getType() : null) .addValue("content", Document.isValid(image) ? image.getContent() : null) .addValue("dataTypeId", validationStamp.getDataType() != null ? validationStamp.getDataType().getDescriptor().getId() : null) .addValue("dataTypeConfig", validationStamp.getDataType() != null ? writeJson(validationStamp.getDataType().getConfig()) : null) ); }