Search in sources :

Example 1 with ProjectEntity

use of net.nemerosa.ontrack.model.structure.ProjectEntity in project ontrack by nemerosa.

the class EventJdbcRepository method post.

@Override
public void post(Event event) {
    StringBuilder sql = new StringBuilder("INSERT INTO EVENTS(EVENT_VALUES, EVENT_TIME, EVENT_USER, EVENT_TYPE, REF");
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("eventValues", writeJson(event.getValues()));
    params.addValue("eventTime", dateTimeForDB(event.getSignature().getTime()));
    params.addValue("eventUser", event.getSignature().getUser().getName());
    params.addValue("eventType", event.getEventType().getId());
    params.addValue("ref", event.getRef() != null ? event.getRef().name() : null);
    for (ProjectEntityType type : event.getEntities().keySet()) {
        sql.append(", ").append(type.name());
    }
    sql.append(") VALUES (:eventValues, :eventTime, :eventUser, :eventType, :ref");
    for (Map.Entry<ProjectEntityType, ProjectEntity> entry : event.getEntities().entrySet()) {
        ProjectEntityType type = entry.getKey();
        ProjectEntity entity = entry.getValue();
        String typeEntry = type.name().toLowerCase();
        sql.append(", :").append(typeEntry);
        params.addValue(typeEntry, entity.id());
    }
    sql.append(")");
    getNamedParameterJdbcTemplate().update(sql.toString(), params);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ProjectEntityType(net.nemerosa.ontrack.model.structure.ProjectEntityType) ProjectEntity(net.nemerosa.ontrack.model.structure.ProjectEntity)

Example 2 with ProjectEntity

use of net.nemerosa.ontrack.model.structure.ProjectEntity in project ontrack by nemerosa.

the class EventJdbcRepository method toEvent.

private Event toEvent(ResultSet rs, BiFunction<ProjectEntityType, ID, ProjectEntity> entityLoader, Function<String, EventType> eventTypeLoader) throws SQLException {
    // Event type name
    String eventTypeName = rs.getString("event_type");
    // Signature
    Signature signature = readSignature(rs, "event_time", "event_user");
    // Entities
    Map<ProjectEntityType, ProjectEntity> entities = new LinkedHashMap<>();
    for (ProjectEntityType type : ProjectEntityType.values()) {
        int entityId = rs.getInt(type.name());
        if (!rs.wasNull()) {
            ProjectEntity entity = entityLoader.apply(type, ID.of(entityId));
            entities.put(type, entity);
        }
    }
    // Reference (if any)
    ProjectEntityType refEntity = getEnum(ProjectEntityType.class, rs, "ref");
    // Values
    Map<String, NameValue> values = loadValues(rs);
    // OK
    return new Event(eventTypeLoader.apply(eventTypeName), signature, entities, refEntity, values);
}
Also used : NameValue(net.nemerosa.ontrack.model.support.NameValue) ProjectEntityType(net.nemerosa.ontrack.model.structure.ProjectEntityType) ProjectEntity(net.nemerosa.ontrack.model.structure.ProjectEntity) Signature(net.nemerosa.ontrack.model.structure.Signature) Event(net.nemerosa.ontrack.model.events.Event)

Example 3 with ProjectEntity

use of net.nemerosa.ontrack.model.structure.ProjectEntity in project ontrack by nemerosa.

the class PropertyJdbcRepository method searchByProperty.

@Override
public Collection<ProjectEntity> searchByProperty(String typeName, BiFunction<ProjectEntityType, ID, ProjectEntity> entityLoader, Predicate<TProperty> predicate) {
    return getNamedParameterJdbcTemplate().execute("SELECT * FROM PROPERTIES WHERE TYPE = :type ORDER BY ID DESC", params("type", typeName), (PreparedStatement ps) -> {
        Collection<ProjectEntity> entities = new ArrayList<>();
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            TProperty t = toProperty(rs);
            if (predicate.test(t)) {
                entities.add(entityLoader.apply(t.getEntityType(), t.getEntityId()));
            }
        }
        return entities;
    });
}
Also used : ProjectEntity(net.nemerosa.ontrack.model.structure.ProjectEntity) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with ProjectEntity

use of net.nemerosa.ontrack.model.structure.ProjectEntity in project ontrack by nemerosa.

the class ProjectEntityExtensionController method getInformation.

/**
 * Gets the list of information extensions for an entity
 */
@RequestMapping(value = "information/{entityType}/{id}", method = RequestMethod.GET)
public Resources<EntityInformation> getInformation(@PathVariable ProjectEntityType entityType, @PathVariable ID id) {
    // Gets the entity
    ProjectEntity entity = getEntity(entityType, id);
    // List of informations to return
    List<EntityInformation> informations = extensionManager.getExtensions(EntityInformationExtension.class).stream().map(x -> x.getInformation(entity)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    // OK
    return Resources.of(informations, uri(MvcUriComponentsBuilder.on(getClass()).getInformation(entityType, id)));
}
Also used : EntityInformationExtension(net.nemerosa.ontrack.extension.api.EntityInformationExtension) PathVariable(org.springframework.web.bind.annotation.PathVariable) EntityInformation(net.nemerosa.ontrack.extension.api.model.EntityInformation) StructureService(net.nemerosa.ontrack.model.structure.StructureService) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Action(net.nemerosa.ontrack.model.support.Action) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) ProjectEntityType(net.nemerosa.ontrack.model.structure.ProjectEntityType) ProjectEntityActionExtension(net.nemerosa.ontrack.extension.api.ProjectEntityActionExtension) List(java.util.List) Resources(net.nemerosa.ontrack.ui.resource.Resources) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) EntityInformationExtension(net.nemerosa.ontrack.extension.api.EntityInformationExtension) ProjectEntity(net.nemerosa.ontrack.model.structure.ProjectEntity) Optional(java.util.Optional) ExtensionManager(net.nemerosa.ontrack.extension.api.ExtensionManager) ID(net.nemerosa.ontrack.model.structure.ID) Optional(java.util.Optional) ProjectEntity(net.nemerosa.ontrack.model.structure.ProjectEntity) EntityInformation(net.nemerosa.ontrack.extension.api.model.EntityInformation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ProjectEntity

use of net.nemerosa.ontrack.model.structure.ProjectEntity in project ontrack by nemerosa.

the class DecorationServiceImplTest method base_exception_decoration_generates_default_error_decoration.

@Test
public void base_exception_decoration_generates_default_error_decoration() {
    ProjectEntity projectEntity = mock(ProjectEntity.class);
    DecorationExtension decorator = mock(DecorationExtension.class);
    when(decorator.getDecorations(any(ProjectEntity.class))).thenThrow(new TestBaseException());
    ExtensionManager extensionManager = mock(ExtensionManager.class);
    when(extensionManager.getExtensions(DecorationExtension.class)).thenReturn(Collections.singletonList(decorator));
    SecurityService securityService = mock(SecurityService.class);
    DecorationServiceImpl service = new DecorationServiceImpl(extensionManager, securityService);
    @SuppressWarnings("unchecked") List<? extends Decoration> decorations = service.getDecorations(projectEntity, decorator);
    assertNotNull(decorations);
    assertEquals(1, decorations.size());
    Decoration decoration = decorations.get(0);
    assertEquals("Known exception", decoration.getError());
    assertNull(decoration.getData());
}
Also used : ProjectEntity(net.nemerosa.ontrack.model.structure.ProjectEntity) ExtensionManager(net.nemerosa.ontrack.extension.api.ExtensionManager) SecurityService(net.nemerosa.ontrack.model.security.SecurityService) DecorationExtension(net.nemerosa.ontrack.extension.api.DecorationExtension) Decoration(net.nemerosa.ontrack.model.structure.Decoration) Test(org.junit.Test)

Aggregations

ProjectEntity (net.nemerosa.ontrack.model.structure.ProjectEntity)6 ExtensionManager (net.nemerosa.ontrack.extension.api.ExtensionManager)3 ProjectEntityType (net.nemerosa.ontrack.model.structure.ProjectEntityType)3 DecorationExtension (net.nemerosa.ontrack.extension.api.DecorationExtension)2 SecurityService (net.nemerosa.ontrack.model.security.SecurityService)2 Decoration (net.nemerosa.ontrack.model.structure.Decoration)2 Test (org.junit.Test)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 EntityInformationExtension (net.nemerosa.ontrack.extension.api.EntityInformationExtension)1 ProjectEntityActionExtension (net.nemerosa.ontrack.extension.api.ProjectEntityActionExtension)1 EntityInformation (net.nemerosa.ontrack.extension.api.model.EntityInformation)1 Event (net.nemerosa.ontrack.model.events.Event)1 ID (net.nemerosa.ontrack.model.structure.ID)1 Signature (net.nemerosa.ontrack.model.structure.Signature)1 StructureService (net.nemerosa.ontrack.model.structure.StructureService)1