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);
}
}
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;
}
Aggregations