Search in sources :

Example 1 with EntityChange

use of org.finos.legend.sdlc.domain.model.entity.change.EntityChange in project legend-sdlc by finos.

the class GitLabEntityApi method validateEntityChanges.

private static void validateEntityChanges(List<? extends EntityChange> entityChanges) {
    StringBuilder builder = new StringBuilder();
    List<String> errorMessages = Lists.mutable.ofInitialCapacity(4);
    int i = 0;
    for (EntityChange change : entityChanges) {
        collectErrorsForEntityChange(change, errorMessages);
        if (!errorMessages.isEmpty()) {
            if (builder.length() == 0) {
                builder.append("There are entity change errors:");
            }
            builder.append("\n\tEntity change #").append(i + 1).append(" (").append(change).append("):");
            errorMessages.forEach(m -> builder.append("\n\t\t").append(m));
            errorMessages.clear();
        }
        i++;
    }
    if (builder.length() > 0) {
        throw new LegendSDLCServerException(builder.toString(), Status.BAD_REQUEST);
    }
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) EntityChange(org.finos.legend.sdlc.domain.model.entity.change.EntityChange)

Example 2 with EntityChange

use of org.finos.legend.sdlc.domain.model.entity.change.EntityChange in project legend-sdlc by finos.

the class GitLabEntityApi method updateEntities.

private Revision updateEntities(String projectId, String workspaceId, Iterable<? extends Entity> newEntities, boolean replace, String message, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType) {
    Map<String, Entity> newEntityDefinitions = Maps.mutable.empty();
    List<String> errorMessages = Lists.mutable.empty();
    for (Entity entity : newEntities) {
        if (entity == null) {
            errorMessages.add("Invalid entity: null");
            continue;
        }
        String path = entity.getPath();
        if (!isValidEntityPath(path)) {
            errorMessages.add("Invalid entity path: \"" + path + "\"");
            continue;
        }
        String classifierPath = entity.getClassifierPath();
        if (!isValidClassifierPath(classifierPath)) {
            errorMessages.add("Entity: " + path + "; error: invalid classifier path \"" + classifierPath + "\"");
        }
        Map<String, ?> content = entity.getContent();
        if (content == null) {
            errorMessages.add("Entity: " + path + "; error: missing content");
        } else if (path != null) {
            Object pkg = content.get("package");
            Object name = content.get("name");
            if (!(pkg instanceof String) || !(name instanceof String) || !path.equals(pkg + "::" + name)) {
                StringBuilder builder = new StringBuilder("Entity: ").append(path).append("; mismatch between entity path and package (");
                if (pkg instanceof String) {
                    builder.append('"').append(pkg).append('"');
                } else {
                    builder.append(pkg);
                }
                builder.append(") and name (");
                if (name instanceof String) {
                    builder.append('"').append(name).append('"');
                } else {
                    builder.append(name);
                }
                builder.append(") properties");
                errorMessages.add(builder.toString());
            }
        }
        Entity oldDefinition = newEntityDefinitions.put(path, entity);
        if (oldDefinition != null) {
            errorMessages.add("Entity: " + path + "; error: multiple definitions");
        }
    }
    if (!errorMessages.isEmpty()) {
        throw new LegendSDLCServerException((errorMessages.size() == 1) ? errorMessages.get(0) : "There are errors with entity definitions:\n\t" + String.join("\n\t", errorMessages), Status.BAD_REQUEST);
    }
    Revision currentWorkspaceRevision = getProjectFileAccessProvider().getRevisionAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType).getCurrentRevision();
    if (currentWorkspaceRevision == null) {
        throw new LegendSDLCServerException("Could not find current revision for " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId + ": " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " may be corrupt");
    }
    String revisionId = currentWorkspaceRevision.getId();
    LOGGER.debug("Using revision {} for reference in entity update in {} {} in project {}", revisionId, workspaceType.getLabel() + " " + workspaceAccessType.getLabel(), workspaceId, projectId);
    List<EntityChange> entityChanges = Lists.mutable.ofInitialCapacity(newEntityDefinitions.size());
    if (newEntityDefinitions.isEmpty()) {
        if (replace) {
            try (Stream<EntityProjectFile> stream = getEntityProjectFiles(projectId, workspaceId, revisionId, workspaceType, workspaceAccessType)) {
                stream.map(EntityProjectFile::getEntityPath).map(EntityChange::newDeleteEntity).forEach(entityChanges::add);
            }
        }
    } else {
        try (Stream<EntityProjectFile> stream = getEntityProjectFiles(projectId, workspaceId, revisionId, workspaceType, workspaceAccessType)) {
            stream.forEach(epf -> {
                String path = epf.getEntityPath();
                Entity newDefinition = newEntityDefinitions.remove(path);
                if (newDefinition == null) {
                    if (replace) {
                        entityChanges.add(EntityChange.newDeleteEntity(path));
                    }
                } else {
                    Entity entity = epf.getEntity();
                    String newClassifierPath = newDefinition.getClassifierPath();
                    Map<String, ?> newContent = newDefinition.getContent();
                    if (!newClassifierPath.equals(entity.getClassifierPath()) || !newContent.equals(entity.getContent())) {
                        entityChanges.add(EntityChange.newModifyEntity(path, newClassifierPath, newContent));
                    }
                }
            });
        }
        newEntityDefinitions.values().forEach(definition -> entityChanges.add(EntityChange.newCreateEntity(definition.getPath(), definition.getClassifierPath(), definition.getContent())));
    }
    return performChanges(projectId, workspaceId, revisionId, message, entityChanges, workspaceType, workspaceAccessType);
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) EntityChange(org.finos.legend.sdlc.domain.model.entity.change.EntityChange) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Revision(org.finos.legend.sdlc.domain.model.revision.Revision)

Aggregations

EntityChange (org.finos.legend.sdlc.domain.model.entity.change.EntityChange)2 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)2 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)1 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)1