public EventCannotRenderEntityException(String template, ProjectEntity projectEntity) { super("Cannot render entity %s for event: %s", projectEntity.getProjectEntityType(), template); } }
public EventBuilder with(ProjectEntity entity) { entities.put(entity.getProjectEntityType(), entity); return this; }
@Override public URI getEntityURI(ProjectEntity entity) { return URI.create( format( "urn:test:entity:%s:%d", entity.getProjectEntityType().name(), entity.id() ) ); }
/** * Depends on the nature of the entity. Allowed to the ones who can create the associated entity. */ @Override public boolean canEdit(ProjectEntity entity, SecurityService securityService) { switch (entity.getProjectEntityType()) { case BUILD: return securityService.isProjectFunctionGranted(entity, BuildCreate.class); case PROMOTION_RUN: return securityService.isProjectFunctionGranted(entity, PromotionRunCreate.class); case VALIDATION_RUN: return securityService.isProjectFunctionGranted(entity, ValidationRunCreate.class); default: throw new PropertyUnsupportedEntityTypeException(getClass().getName(), entity.getProjectEntityType()); } }
@Override public List<Decoration<?>> getDecorations(ProjectEntity entity) { // Downloading a decoration with the current security context Function<Decorator, Stream<Decoration<?>>> securedDecoratorFunction = securityService.runner( decorator -> getDecorations(entity, decorator).stream() ); // OK return extensionManager.getExtensions(DecorationExtension.class) .stream() // ... and filters per entity .filter(decorator -> decorator.getScope().contains(entity.getProjectEntityType())) // ... and gets the decoration .flatMap(securedDecoratorFunction) // OK .collect(Collectors.toList()); }
@Override public List<Property<?>> getProperties(ProjectEntity entity) { // With all the existing properties... return getPropertyTypes().stream() // ... filters them by entity .filter(type -> type.getSupportedEntityTypes().contains(entity.getProjectEntityType())) // ... filters them by access right .filter(type -> type.canView(entity, securityService)) // ... loads them from the store .map(type -> getProperty(type, entity)) // .. flags with editionrights .map(prop -> prop.editable(prop.getType().canEdit(entity, securityService))) // ... and returns them .collect(Collectors.toList()); }
protected <T> T getPropertyValue(PropertyType<T> type, ProjectEntity entity) { // Supported entity? if (!type.getSupportedEntityTypes().contains(entity.getProjectEntityType())) { throw new PropertyUnsupportedEntityTypeException(type.getClass().getName(), entity.getProjectEntityType()); } // Checks for viewing if (!type.canView(entity, securityService)) { throw new AccessDeniedException("Property is not opened for viewing."); } // Gets the raw information from the repository TProperty t = propertyRepository.loadProperty( type.getClass().getName(), entity.getProjectEntityType(), entity.getId()); // If null, returns null if (t == null) { return null; } // Converts the stored value into an actual value return type.fromStorage(t.getJson()); }
public EventBuilder withRef(ProjectEntity entity) { this.ref = entity.getProjectEntityType(); return withProject(entity.getProject()).with(entity); }
@Override public URI getEntityPage(ProjectEntity entity) { return page("entity:%s:%d", entity.getProjectEntityType().name(), entity.id()); }
@Override public int getCountByCategory(ProjectEntity entity, String category) { return getNamedParameterJdbcTemplate().queryForObject( String.format( "SELECT COUNT(*) FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND CATEGORY = :category ", entity.getProjectEntityType().name() ), params("entityId", entity.id()) .addValue("category", category), Integer.class ); }
@Override public Optional<EntityDataStoreRecord> getById(ProjectEntity entity, int id) { return getOptional( String.format( "SELECT * FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND ID = :id", entity.getProjectEntityType().name() ), params("id", id).addValue("entityId", entity.id()), (rs, rowNum) -> toEntityDataStoreRecord(entity, rs) ); }
@Override public int getCountByCategoryAndName(ProjectEntity entity, String category, String name) { return getNamedParameterJdbcTemplate().queryForObject( String.format( "SELECT COUNT(*) FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND CATEGORY = :category " + "AND NAME = :name ", entity.getProjectEntityType().name() ), params("entityId", entity.id()) .addValue("category", category) .addValue("name", name), Integer.class ); }
private <T> Ack editProperty(ProjectEntity entity, PropertyType<T> propertyType, T value) { // Checks for edition if (!propertyType.canEdit(entity, securityService)) { throw new AccessDeniedException("Property is not opened for edition."); } // Gets the JSON for the storage JsonNode storage = propertyType.forStorage(value); // Stores the property propertyRepository.saveProperty( propertyType.getClass().getName(), entity.getProjectEntityType(), entity.getId(), storage ); // Property change event eventPostService.post(eventFactory.propertyChange(entity, propertyType)); // Listener propertyType.onPropertyChanged(entity, value); // OK return Ack.OK; }
@Override public void deleteByName(ProjectEntity entity, String category, String name) { getNamedParameterJdbcTemplate().update( String.format( "DELETE FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND CATEGORY = :category " + "AND NAME = :name", entity.getProjectEntityType().name() ), params("entityId", entity.id()) .addValue("category", category) .addValue("name", name) ); }
@Override public void deleteByGroup(ProjectEntity entity, String category, String groupName) { getNamedParameterJdbcTemplate().update( String.format( "DELETE FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND CATEGORY = :category " + "AND GROUPID = :groupId", entity.getProjectEntityType().name() ), params("entityId", entity.id()) .addValue("category", category) .addValue("groupId", groupName) ); }
@Override public List<EntityDataStoreRecord> findLastRecordsByNameInCategory(ProjectEntity entity, String category) { return getLastByName( getNamedParameterJdbcTemplate().query( String.format( "SELECT * FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND CATEGORY = :category " + "ORDER BY CREATION DESC, ID DESC", entity.getProjectEntityType().name() ), params("entityId", entity.id()) .addValue("category", category), (rs, rowNum) -> toEntityDataStoreRecord(entity, rs) ) ); }
@Override public List<EntityDataStoreRecord> getByCategory(ProjectEntity entity, String category, int offset, int page) { return getNamedParameterJdbcTemplate().query( String.format( "SELECT * FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND CATEGORY = :category " + "ORDER BY CREATION DESC, ID DESC " + "LIMIT :page OFFSET :offset", entity.getProjectEntityType().name() ), params("entityId", entity.id()) .addValue("category", category) .addValue("offset", offset) .addValue("page", page), (rs, rowNum) -> toEntityDataStoreRecord(entity, rs) ); }
@Override public List<EntityDataStoreRecord> getByCategoryAndName(ProjectEntity entity, String category, String name, int offset, int page) { return getNamedParameterJdbcTemplate().query( String.format( "SELECT * FROM ENTITY_DATA_STORE " + "WHERE %s = :entityId " + "AND CATEGORY = :category " + "AND NAME = :name " + "ORDER BY CREATION DESC, ID DESC " + "LIMIT :page OFFSET :offset", entity.getProjectEntityType().name() ), params("entityId", entity.id()) .addValue("category", category) .addValue("name", name) .addValue("offset", offset) .addValue("page", page), (rs, rowNum) -> toEntityDataStoreRecord(entity, rs) ); }
@Override public <T> Event propertyChange(ProjectEntity entity, PropertyType<T> propertyType) { return Event.of(PROPERTY_CHANGE) .withRef(entity) .with("entity", entity.getProjectEntityType().getDisplayName()) .with("property", new NameValue( propertyType.getTypeName(), propertyType.getName() )) .get(); }
@Override public <T> Event propertyDelete(ProjectEntity entity, PropertyType<T> propertyType) { return Event.of(PROPERTY_DELETE) .withRef(entity) .with("entity", entity.getProjectEntityType().getDisplayName()) .with("property", new NameValue( propertyType.getTypeName(), propertyType.getName() )) .get(); }