Search in sources :

Example 6 with MetadataFacet

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);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Node(javax.jcr.Node) Calendar(java.util.Calendar) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 7 with MetadataFacet

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;
}
Also used : MetadataFacetModel(org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory) MailingList(org.apache.archiva.metadata.model.MailingList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with MetadataFacet

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;
}
Also used : HashMap(java.util.HashMap) OrderedRows(me.prettyprint.hector.api.beans.OrderedRows) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory)

Example 9 with 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();
        }
    }
}
Also used : MetadataFacetModel(org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel) OrderedRows(me.prettyprint.hector.api.beans.OrderedRows) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with MetadataFacet

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;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) HashMap(java.util.HashMap) Node(javax.jcr.Node) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) Property(javax.jcr.Property) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory)

Aggregations

MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)25 HashMap (java.util.HashMap)19 GenericMetadataFacet (org.apache.archiva.metadata.generic.GenericMetadataFacet)11 MetadataFacetFactory (org.apache.archiva.metadata.model.MetadataFacetFactory)10 Map (java.util.Map)8 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)8 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)8 Test (org.junit.Test)7 Node (javax.jcr.Node)6 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)6 ArrayList (java.util.ArrayList)5 RepositoryException (javax.jcr.RepositoryException)5 MailingList (org.apache.archiva.metadata.model.MailingList)4 IOException (java.io.IOException)3 Properties (java.util.Properties)3 Property (javax.jcr.Property)3 OrderedRows (me.prettyprint.hector.api.beans.OrderedRows)3 CiManagement (org.apache.archiva.metadata.model.CiManagement)3 Dependency (org.apache.archiva.metadata.model.Dependency)3 IssueManagement (org.apache.archiva.metadata.model.IssueManagement)3