Search in sources :

Example 1 with UpdateWithAssignments

use of com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments in project archiva by apache.

the class CassandraMetadataRepository method updateProjectVersion.

@Override
public void updateProjectVersion(RepositorySession repositorySession, String repositoryId, String namespaceId, String projectId, ProjectVersionMetadata versionMetadata) throws MetadataRepositoryException {
    try {
        Namespace namespace = getNamespace(repositoryId, namespaceId);
        if (namespace == null) {
            updateOrAddNamespace(repositoryId, namespaceId);
        }
        if (getProject(repositorySession, repositoryId, namespaceId, projectId) == null) {
            ProjectMetadata projectMetadata = new ProjectMetadata();
            projectMetadata.setNamespace(namespaceId);
            projectMetadata.setId(projectId);
            updateProject(repositorySession, repositoryId, projectMetadata);
        }
    } catch (MetadataResolutionException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    CqlSession session = cassandraArchivaManager.getSession();
    {
        String table = cassandraArchivaManager.getProjectVersionMetadataFamilyName();
        Select query = selectFrom(table).column(PROJECT_ID.toString()).all().whereColumn(REPOSITORY_NAME.toString()).isEqualTo(literal(repositoryId)).whereColumn(NAMESPACE_ID.toString()).isEqualTo(literal(namespaceId)).whereColumn(PROJECT_ID.toString()).isEqualTo(literal(projectId)).whereColumn(PROJECT_VERSION.toString()).isEqualTo(literal(versionMetadata.getId())).allowFiltering();
        ProjectVersionMetadataModel projectVersionMetadataModel;
        boolean create = true;
        Row result = session.execute(query.build()).one();
        if (result != null) {
            projectVersionMetadataModel = mapProjectVersionMetadataModel(result);
            create = false;
        } else {
            projectVersionMetadataModel = getModelMapper().map(versionMetadata, ProjectVersionMetadataModel.class);
        }
        projectVersionMetadataModel.setProjectId(projectId);
        projectVersionMetadataModel.setNamespace(new Namespace(namespaceId, new Repository(repositoryId)));
        projectVersionMetadataModel.setCiManagement(versionMetadata.getCiManagement());
        projectVersionMetadataModel.setIssueManagement(versionMetadata.getIssueManagement());
        projectVersionMetadataModel.setOrganization(versionMetadata.getOrganization());
        projectVersionMetadataModel.setScm(versionMetadata.getScm());
        projectVersionMetadataModel.setMailingLists(versionMetadata.getMailingLists());
        projectVersionMetadataModel.setDependencies(versionMetadata.getDependencies());
        projectVersionMetadataModel.setLicenses(versionMetadata.getLicenses());
        // we don't test, if repository and namespace really exist !
        String key = // 
        new ProjectVersionMetadataModel.KeyBuilder().withRepository(// 
        repositoryId).withNamespace(// 
        namespaceId).withProjectId(// 
        projectId).withProjectVersion(// 
        versionMetadata.getVersion()).withId(// 
        versionMetadata.getId()).build();
        // Update is upsert
        table = cassandraArchivaManager.getProjectVersionMetadataFamilyName();
        UpdateWithAssignments update = update(table).setColumn(PROJECT_ID.toString(), literal(projectId)).setColumn(REPOSITORY_NAME.toString(), literal(repositoryId)).setColumn(NAMESPACE_ID.toString(), literal(namespaceId)).setColumn(PROJECT_VERSION.toString(), literal(versionMetadata.getVersion())).setColumn(DESCRIPTION.toString(), literal(versionMetadata.getDescription())).setColumn(NAME.toString(), literal(versionMetadata.getName())).setColumn("incomplete", literal(versionMetadata.isIncomplete())).setColumn(URL.toString(), literal(versionMetadata.getUrl()));
        {
            CiManagement ci = versionMetadata.getCiManagement();
            if (ci != null) {
                update = update.setColumn("\"ciManagement.system\"", literal(ci.getSystem())).setColumn("\"ciManagement.url\"", literal(ci.getUrl()));
            }
        }
        {
            IssueManagement issueManagement = versionMetadata.getIssueManagement();
            if (issueManagement != null) {
                update = update.setColumn("\"issueManagement.system\"", literal(issueManagement.getSystem())).setColumn("\"issueManagement.url\"", literal(issueManagement.getUrl()));
            }
        }
        {
            Organization organization = versionMetadata.getOrganization();
            if (organization != null) {
                update = update.setColumn("\"organization.name\"", literal(organization.getName())).setColumn("\"organization.url\"", literal(organization.getUrl()));
            }
        }
        {
            Scm scm = versionMetadata.getScm();
            if (scm != null) {
                update = update.setColumn("\"scm.url\"", literal(scm.getUrl())).setColumn("\"scm.connection\"", literal(scm.getConnection())).setColumn("\"scm.developerConnection\"", literal(scm.getDeveloperConnection()));
            }
        }
        if (versionMetadata.getProperties() != null && versionMetadata.getProperties().size() > 0) {
            for (Map.Entry<String, String> entry : versionMetadata.getProperties().entrySet()) {
                update = update.setMapValue(VERSION_PROPERTIES.toString(), literal(entry.getKey()), literal(entry.getValue()));
            }
        }
        Update finalUpdate = update.whereColumn(DEFAULT_PRIMARY_KEY).isEqualTo(literal(key));
        session.execute(finalUpdate.build());
        if (!create) {
            removeMailingList(key);
            removeLicenses(key);
            removeDependencies(key);
        }
        recordMailingList(key, versionMetadata.getMailingLists());
        recordLicenses(key, versionMetadata.getLicenses());
        recordDependencies(key, versionMetadata.getDependencies(), repositoryId);
        ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
        artifactMetadataModel.setRepositoryId(repositoryId);
        artifactMetadataModel.setNamespace(namespaceId);
        artifactMetadataModel.setProject(projectId);
        artifactMetadataModel.setProjectVersion(versionMetadata.getVersion());
        artifactMetadataModel.setVersion(versionMetadata.getVersion());
        updateFacets(versionMetadata, artifactMetadataModel);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ArtifactMetadataModel(org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel) Organization(org.apache.archiva.metadata.model.Organization) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) Update(com.datastax.oss.driver.api.querybuilder.update.Update) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Namespace(org.apache.archiva.metadata.repository.cassandra.model.Namespace) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) ProjectVersionMetadataModel(org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Repository(org.apache.archiva.metadata.repository.cassandra.model.Repository) AbstractMetadataRepository(org.apache.archiva.metadata.repository.AbstractMetadataRepository) ProjectMetadata(org.apache.archiva.metadata.model.ProjectMetadata) Select(com.datastax.oss.driver.api.querybuilder.select.Select) CiManagement(org.apache.archiva.metadata.model.CiManagement) Row(com.datastax.oss.driver.api.core.cql.Row) Scm(org.apache.archiva.metadata.model.Scm) IssueManagement(org.apache.archiva.metadata.model.IssueManagement)

Example 2 with UpdateWithAssignments

use of com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments in project spring-data-cassandra by spring-projects.

the class StatementFactory method update.

/**
 * Create an {@literal UPDATE} statement by mapping {@code objectToUpdate} to {@link Update} considering
 * {@link UpdateOptions}.
 *
 * @param objectToUpdate must not be {@literal null}.
 * @param options must not be {@literal null}.
 * @param entity must not be {@literal null}.
 * @param tableName must not be {@literal null}.
 * @return the update builder.
 */
StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> update(Object objectToUpdate, WriteOptions options, CassandraPersistentEntity<?> entity, CqlIdentifier tableName) {
    Assert.notNull(tableName, "TableName must not be null");
    Assert.notNull(objectToUpdate, "Object to builder must not be null");
    Assert.notNull(options, "WriteOptions must not be null");
    Assert.notNull(entity, "CassandraPersistentEntity must not be null");
    Where where = new Where();
    cassandraConverter.write(objectToUpdate, where, entity);
    Map<CqlIdentifier, Object> object = new LinkedHashMap<>();
    cassandraConverter.write(objectToUpdate, object, entity);
    where.forEach((cqlIdentifier, o) -> object.remove(cqlIdentifier));
    StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> builder = StatementBuilder.of(QueryBuilder.update(tableName).set().where()).bind((statement, factory) -> ((UpdateWithAssignments) statement).set(toAssignments(object, factory)).where(toRelations(where, factory))).apply(update -> addWriteOptions(update, options));
    Optional.of(options).filter(UpdateOptions.class::isInstance).map(UpdateOptions.class::cast).map(UpdateOptions::getIfCondition).ifPresent(criteriaDefinitions -> applyUpdateIfCondition(builder, criteriaDefinitions));
    builder.transform(statement -> QueryOptionsUtil.addQueryOptions(statement, options));
    return builder;
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) TermFactory(org.springframework.data.cassandra.core.cql.util.TermFactory) Relation(com.datastax.oss.driver.api.querybuilder.relation.Relation) Assignment(com.datastax.oss.driver.api.querybuilder.update.Assignment) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) SetAtIndexOp(org.springframework.data.cassandra.core.query.Update.SetAtIndexOp) SetOp(org.springframework.data.cassandra.core.query.Update.SetOp) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) EntityProjection(org.springframework.data.projection.EntityProjection) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) PersistentPropertyTranslator(org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator) Predicates(org.springframework.data.util.Predicates) Map(java.util.Map) RemoveOp(org.springframework.data.cassandra.core.query.Update.RemoveOp) Sort(org.springframework.data.domain.Sort) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Select(com.datastax.oss.driver.api.querybuilder.select.Select) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BindMarker(com.datastax.oss.driver.api.querybuilder.BindMarker) ClassUtils(org.springframework.util.ClassUtils) ColumnRelationBuilder(com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder) Collection(java.util.Collection) Set(java.util.Set) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate) Collectors(java.util.stream.Collectors) Selector(org.springframework.data.cassandra.core.query.Columns.Selector) List(java.util.List) UpdateMapper(org.springframework.data.cassandra.core.convert.UpdateMapper) PropertyDescriptor(java.beans.PropertyDescriptor) QueryMapper(org.springframework.data.cassandra.core.convert.QueryMapper) Optional(java.util.Optional) AddToOp(org.springframework.data.cassandra.core.query.Update.AddToOp) NonNull(org.springframework.lang.NonNull) AssignmentOp(org.springframework.data.cassandra.core.query.Update.AssignmentOp) IncrOp(org.springframework.data.cassandra.core.query.Update.IncrOp) SetAtKeyOp(org.springframework.data.cassandra.core.query.Update.SetAtKeyOp) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) AddToMapOp(org.springframework.data.cassandra.core.query.Update.AddToMapOp) UpdateStart(com.datastax.oss.driver.api.querybuilder.update.UpdateStart) OngoingAssignment(com.datastax.oss.driver.api.querybuilder.update.OngoingAssignment) Condition(com.datastax.oss.driver.api.querybuilder.condition.Condition) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Filter(org.springframework.data.cassandra.core.query.Filter) Function(java.util.function.Function) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) StatementBuilder(org.springframework.data.cassandra.core.cql.util.StatementBuilder) Nullable(org.springframework.lang.Nullable) Mode(org.springframework.data.cassandra.core.query.Update.AddToOp.Mode) ConditionBuilder(com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder) EntityWriter(org.springframework.data.convert.EntityWriter) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Update(org.springframework.data.cassandra.core.query.Update) CassandraConverter(org.springframework.data.cassandra.core.convert.CassandraConverter) Query(org.springframework.data.cassandra.core.query.Query) QueryOptionsUtil(org.springframework.data.cassandra.core.cql.QueryOptionsUtil) Term(com.datastax.oss.driver.api.querybuilder.term.Term) Collections(java.util.Collections) Where(org.springframework.data.cassandra.core.convert.Where) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) Update(org.springframework.data.cassandra.core.query.Update) Where(org.springframework.data.cassandra.core.convert.Where) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Select (com.datastax.oss.driver.api.querybuilder.select.Select)2 UpdateWithAssignments (com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 Row (com.datastax.oss.driver.api.core.cql.Row)1 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)1 BindMarker (com.datastax.oss.driver.api.querybuilder.BindMarker)1 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)1 Condition (com.datastax.oss.driver.api.querybuilder.condition.Condition)1 ConditionBuilder (com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder)1 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)1 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)1 Insert (com.datastax.oss.driver.api.querybuilder.insert.Insert)1 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)1 ColumnRelationBuilder (com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder)1 Relation (com.datastax.oss.driver.api.querybuilder.relation.Relation)1 Term (com.datastax.oss.driver.api.querybuilder.term.Term)1 Assignment (com.datastax.oss.driver.api.querybuilder.update.Assignment)1 OngoingAssignment (com.datastax.oss.driver.api.querybuilder.update.OngoingAssignment)1 Update (com.datastax.oss.driver.api.querybuilder.update.Update)1