use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class DefaultBrowseService method addMetadata.
@Override
public Boolean addMetadata(String groupId, String artifactId, String version, String key, String value, String repositoryId) throws ArchivaRestServiceException {
ProjectVersionMetadata projectVersionMetadata = getProjectMetadata(groupId, artifactId, version, repositoryId);
if (projectVersionMetadata == null) {
return Boolean.FALSE;
}
Map<String, String> properties = new HashMap<>();
MetadataFacet metadataFacet = projectVersionMetadata.getFacet(GenericMetadataFacet.FACET_ID);
if (metadataFacet != null && metadataFacet.toProperties() != null) {
properties.putAll(metadataFacet.toProperties());
} else {
metadataFacet = new GenericMetadataFacet();
}
properties.put(key, value);
metadataFacet.fromProperties(properties);
projectVersionMetadata.addFacet(metadataFacet);
RepositorySession repositorySession = repositorySessionFactory.createSession();
try {
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.updateProjectVersion(repositoryId, groupId, artifactId, projectVersionMetadata);
repositorySession.save();
} catch (MetadataRepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} finally {
repositorySession.close();
}
return Boolean.TRUE;
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class DefaultBrowseService method getMetadatas.
@Override
public List<Entry> getMetadatas(String groupId, String artifactId, String version, String repositoryId) throws ArchivaRestServiceException {
ProjectVersionMetadata projectVersionMetadata = getProjectMetadata(groupId, artifactId, version, repositoryId);
if (projectVersionMetadata == null) {
return Collections.emptyList();
}
MetadataFacet metadataFacet = projectVersionMetadata.getFacet(GenericMetadataFacet.FACET_ID);
if (metadataFacet == null) {
return Collections.emptyList();
}
Map<String, String> map = metadataFacet.toProperties();
List<Entry> entries = new ArrayList<>(map.size());
for (Map.Entry<String, String> entry : map.entrySet()) {
entries.add(new Entry(entry.getKey(), entry.getValue()));
}
return entries;
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class AbstractMetadataRepositoryTest method testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved.
@Test
public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved() throws Exception {
ArtifactMetadata metadata = createArtifact();
Map<String, String> additionalProps = new HashMap<>();
additionalProps.put("deleteKey", "deleteValue");
MetadataFacet facet = new TestMetadataFacet(TEST_FACET_ID, "baz", additionalProps);
metadata.addFacet(facet);
repository.updateArtifact(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
Collection<ArtifactMetadata> artifacts = repository.getArtifacts(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
metadata = artifacts.iterator().next();
Collection<String> ids = metadata.getFacetIds();
assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
Map<String, String> facetProperties = testFacet.toProperties();
assertEquals("deleteValue", facetProperties.get("deleteKey"));
facetProperties.remove("deleteKey");
TestMetadataFacet newTestFacet = new TestMetadataFacet(TEST_FACET_ID, testFacet.getValue(), facetProperties);
metadata.addFacet(newTestFacet);
repository.updateArtifact(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
artifacts = repository.getArtifacts(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
metadata = artifacts.iterator().next();
ids = metadata.getFacetIds();
assertThat(ids).isNotNull().isNotEmpty().hasSize(1).contains(TEST_FACET_ID);
testFacet = (TestMetadataFacet) metadata.getFacet(TEST_FACET_ID);
Map<String, String> props = testFacet.toProperties();
assertThat(props).isNotNull().doesNotContainKey("deleteKey");
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class AbstractMetadataRepositoryTest method testGetMetadataFacetWhenDefaultValue.
@Test
public void testGetMetadataFacetWhenDefaultValue() throws Exception {
repository.addMetadataFacet(TEST_REPO_ID, new TestMetadataFacet(null));
MetadataFacet metadataFacet = repository.getMetadataFacet(TEST_REPO_ID, TEST_FACET_ID, TEST_NAME);
assertEquals(new TestMetadataFacet(TEST_METADATA_VALUE), metadataFacet);
}
use of org.apache.archiva.metadata.model.MetadataFacet in project archiva by apache.
the class AbstractMetadataRepositoryTest method testGetArtifactsByMetadataAllRepositories.
@Test
public void testGetArtifactsByMetadataAllRepositories() throws Exception {
createArtifactWithMavenArtifactFacet();
tryAssert(() -> {
Collection<ArtifactMetadata> artifactsByMetadata = repository.getArtifactsByMetadata("foo", TEST_METADATA_VALUE, null);
assertThat(artifactsByMetadata).hasSize(1);
ArtifactMetadata artifactMetadata = artifactsByMetadata.iterator().next();
assertThat(artifactMetadata.getId()).isEqualTo("projectId-1.0.jar");
assertThat(artifactMetadata.getSha1()).isEqualTo(TEST_SHA1);
assertThat(artifactMetadata.getRepositoryId()).isEqualTo(TEST_REPO_ID);
MetadataFacet facet = artifactMetadata.getFacet(TEST_FACET_ID);
assertThat(facet).isNotNull();
assertThat(facet.toProperties()).isEqualTo(Collections.singletonMap("foo", TEST_METADATA_VALUE));
});
}
Aggregations