use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class JcrMetadataRepository method updateArtifact.
@Override
public void updateArtifact(String repositoryId, String namespace, String projectId, String projectVersion, ArtifactMetadata artifactMeta) throws MetadataRepositoryException {
updateNamespace(repositoryId, namespace);
try {
Node node = getOrAddArtifactNode(repositoryId, namespace, projectId, projectVersion, artifactMeta.getId());
Calendar cal = Calendar.getInstance();
cal.setTime(artifactMeta.getFileLastModified());
node.setProperty(JCR_LAST_MODIFIED, cal);
cal = Calendar.getInstance();
cal.setTime(artifactMeta.getWhenGathered());
node.setProperty("whenGathered", cal);
node.setProperty("size", artifactMeta.getSize());
node.setProperty("md5", artifactMeta.getMd5());
node.setProperty("sha1", artifactMeta.getSha1());
node.setProperty("version", artifactMeta.getVersion());
// iterate over available facets to update/add/remove from the artifactMetadata
for (String facetId : metadataFacetFactories.keySet()) {
MetadataFacet metadataFacet = artifactMeta.getFacet(facetId);
if (metadataFacet == null) {
continue;
}
if (node.hasNode(facetId)) {
node.getNode(facetId).remove();
}
if (metadataFacet != null) {
// recreate, to ensure properties are removed
Node n = node.addNode(facetId);
n.addMixin(FACET_NODE_TYPE);
for (Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet()) {
n.setProperty(entry.getKey(), entry.getValue());
}
}
}
} catch (RepositoryException e) {
throw new MetadataRepositoryException(e.getMessage(), e);
}
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class CassandraMetadataRepository method mapArtifactMetadataToArtifact.
/**
* Attach metadata to each of the ArtifactMetadata objects
*/
private List<ArtifactMetadata> mapArtifactMetadataToArtifact(QueryResult<OrderedRows<String, String, String>> result, List<ArtifactMetadata> artifactMetadatas) {
if (result.get() == null || result.get().getCount() < 1) {
return artifactMetadatas;
}
final List<MetadataFacetModel> metadataFacetModels = new ArrayList<>(result.get().getCount());
for (Row<String, String, String> row : result.get()) {
ColumnSlice<String, String> columnSlice = row.getColumnSlice();
MetadataFacetModel metadataFacetModel = new MetadataFacetModel();
metadataFacetModel.setFacetId(getStringValue(columnSlice, FACET_ID.toString()));
metadataFacetModel.setName(getStringValue(columnSlice, NAME.toString()));
metadataFacetModel.setValue(getStringValue(columnSlice, VALUE.toString()));
metadataFacetModel.setKey(getStringValue(columnSlice, KEY.toString()));
metadataFacetModel.setProjectVersion(getStringValue(columnSlice, PROJECT_VERSION.toString()));
metadataFacetModels.add(metadataFacetModel);
}
for (final ArtifactMetadata artifactMetadata : artifactMetadatas) {
Iterable<MetadataFacetModel> metadataFacetModelIterable = Iterables.filter(metadataFacetModels, new Predicate<MetadataFacetModel>() {
@Override
public boolean apply(MetadataFacetModel metadataFacetModel) {
if (metadataFacetModel != null) {
return StringUtils.equals(artifactMetadata.getVersion(), metadataFacetModel.getProjectVersion());
}
return false;
}
});
Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId = new HashMap<>();
while (iterator.hasNext()) {
MetadataFacetModel metadataFacetModel = iterator.next();
List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get(metadataFacetModel.getName());
if (values == null) {
values = new ArrayList<>();
metadataFacetValuesPerFacetId.put(metadataFacetModel.getFacetId(), values);
}
values.add(metadataFacetModel);
}
for (Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet()) {
MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get(entry.getKey());
if (metadataFacetFactory != null) {
List<MetadataFacetModel> facetModels = entry.getValue();
if (!facetModels.isEmpty()) {
MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet();
Map<String, String> props = new HashMap<>(facetModels.size());
for (MetadataFacetModel metadataFacetModel : facetModels) {
props.put(metadataFacetModel.getKey(), metadataFacetModel.getValue());
}
metadataFacet.fromProperties(props);
artifactMetadata.addFacet(metadataFacet);
}
}
}
}
return artifactMetadatas;
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class CassandraMetadataRepository method getMetadataFacet.
@Override
public MetadataFacet getMetadataFacet(final String repositoryId, final String facetId, final String name) throws MetadataRepositoryException {
MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get(facetId);
if (metadataFacetFactory == null) {
return null;
}
QueryResult<OrderedRows<String, String, String>> result = //
HFactory.createRangeSlicesQuery(keyspace, ss, ss, //
ss).setColumnFamily(//
cassandraArchivaManager.getMetadataFacetFamilyName()).setColumnNames(KEY.toString(), //
VALUE.toString()).addEqualsExpression(REPOSITORY_NAME.toString(), //
repositoryId).addEqualsExpression(FACET_ID.toString(), //
facetId).addEqualsExpression(NAME.toString(), //
name).execute();
MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet(repositoryId, name);
int size = result.get().getCount();
if (size < 1) {
return null;
}
Map<String, String> map = new HashMap<>(size);
for (Row<String, String, String> row : result.get()) {
ColumnSlice<String, String> columnSlice = row.getColumnSlice();
map.put(getStringValue(columnSlice, KEY.toString()), getStringValue(columnSlice, VALUE.toString()));
}
metadataFacet.fromProperties(map);
return metadataFacet;
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class CassandraMetadataRepository method updateFacets.
/**
* iterate over available facets to remove/add from the artifactMetadata
*
* @param facetedMetadata
* @param artifactMetadataModel only use for the key
*/
private void updateFacets(final FacetedMetadata facetedMetadata, final ArtifactMetadataModel artifactMetadataModel) {
String cf = cassandraArchivaManager.getMetadataFacetFamilyName();
for (final String facetId : metadataFacetFactories.keySet()) {
MetadataFacet metadataFacet = facetedMetadata.getFacet(facetId);
if (metadataFacet == null) {
continue;
}
// clean first
QueryResult<OrderedRows<String, String, String>> result = //
HFactory.createRangeSlicesQuery(keyspace, ss, ss, ss).setColumnFamily(//
cf).setColumnNames(//
REPOSITORY_NAME.toString()).addEqualsExpression(REPOSITORY_NAME.toString(), //
artifactMetadataModel.getRepositoryId()).addEqualsExpression(NAMESPACE_ID.toString(), //
artifactMetadataModel.getNamespace()).addEqualsExpression(PROJECT_ID.toString(), //
artifactMetadataModel.getProject()).addEqualsExpression(PROJECT_VERSION.toString(), //
artifactMetadataModel.getProjectVersion()).addEqualsExpression(FACET_ID.toString(), //
facetId).execute();
for (Row<String, String, String> row : result.get().getList()) {
this.metadataFacetTemplate.deleteRow(row.getKey());
}
Map<String, String> properties = metadataFacet.toProperties();
for (Map.Entry<String, String> entry : properties.entrySet()) {
String key = new MetadataFacetModel.KeyBuilder().withKey(entry.getKey()).withArtifactMetadataModel(artifactMetadataModel).withFacetId(facetId).withName(metadataFacet.getName()).build();
Mutator<String> mutator = //
metadataFacetTemplate.createMutator().addInsertion(key, cf, //
column(REPOSITORY_NAME.toString(), artifactMetadataModel.getRepositoryId())).addInsertion(key, cf, //
column(NAMESPACE_ID.toString(), artifactMetadataModel.getNamespace())).addInsertion(key, cf, //
column(PROJECT_ID.toString(), artifactMetadataModel.getProject())).addInsertion(key, cf, //
column(PROJECT_VERSION.toString(), artifactMetadataModel.getProjectVersion())).addInsertion(key, cf, //
column(FACET_ID.toString(), facetId)).addInsertion(key, cf, //
column(KEY.toString(), entry.getKey())).addInsertion(key, cf, column(VALUE.toString(), entry.getValue()));
if (metadataFacet.getName() != null) {
mutator.addInsertion(key, cf, column(NAME.toString(), metadataFacet.getName()));
}
mutator.execute();
}
}
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class JcrMetadataRepository method getMetadataFacet.
@Override
public MetadataFacet getMetadataFacet(String repositoryId, String facetId, String name) throws MetadataRepositoryException {
MetadataFacet metadataFacet = null;
try {
Node root = getJcrSession().getRootNode();
Node node = root.getNode(getFacetPath(repositoryId, facetId, name));
if (metadataFacetFactories == null) {
return metadataFacet;
}
MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get(facetId);
if (metadataFacetFactory != null) {
metadataFacet = metadataFacetFactory.createMetadataFacet(repositoryId, name);
Map<String, String> map = new HashMap<>();
for (Property property : JcrUtils.getProperties(node)) {
String p = property.getName();
if (!p.startsWith("jcr:")) {
map.put(p, property.getString());
}
}
metadataFacet.fromProperties(map);
}
} catch (PathNotFoundException e) {
// ignored - the facet doesn't exist, so return null
} catch (RepositoryException e) {
throw new MetadataRepositoryException(e.getMessage(), e);
}
return metadataFacet;
}
Aggregations