Skip to content

Commit

Permalink
Merge pull request #1190 from josegar74/feature-custom-metadata-ident…
Browse files Browse the repository at this point in the history
…ifiers

#1189 Metadata Identifier Templates
  • Loading branch information
josegar74 committed Oct 6, 2015
2 parents 0d57494 + 9fa49a4 commit 6d788cf
Show file tree
Hide file tree
Showing 28 changed files with 935 additions and 12 deletions.
25 changes: 23 additions & 2 deletions core/src/main/java/org/fao/geonet/kernel/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ public void apply(Metadata entity) {
//--------------------------------------------------------------------------

/**
* Creates a new metadata duplicating an existing template.
* Creates a new metadata duplicating an existing template creating a random uuid.
*
* @param context
* @param templateId
Expand All @@ -1510,14 +1510,35 @@ public void apply(Metadata entity) {
public String createMetadata(ServiceContext context, String templateId, String groupOwner,
String source, int owner,
String parentUuid, String isTemplate, boolean fullRightsForGroup) throws Exception {

return createMetadata(context, templateId, groupOwner, source,
owner, parentUuid, isTemplate, fullRightsForGroup, UUID.randomUUID().toString());
}

/**
* Creates a new metadata duplicating an existing template with an specified uuid.
*
* @param context
* @param templateId
* @param groupOwner
* @param source
* @param owner
* @param parentUuid
* @param isTemplate TODO
* @param fullRightsForGroup TODO
* @return
* @throws Exception
*/
public String createMetadata(ServiceContext context, String templateId, String groupOwner,
String source, int owner,
String parentUuid, String isTemplate, boolean fullRightsForGroup, String uuid) throws Exception {
Metadata templateMetadata = getMetadataRepository().findOne(templateId);
if (templateMetadata == null) {
throw new IllegalArgumentException("Template id not found : " + templateId);
}

String schema = templateMetadata.getDataInfo().getSchemaId();
String data = templateMetadata.getData();
String uuid = UUID.randomUUID().toString();
Element xml = Xml.loadString(data, false);
if (templateMetadata.getDataInfo().getType() == MetadataType.METADATA) {
xml = updateFixedInfo(schema, Optional.<Integer>absent(), uuid, xml, parentUuid, UpdateDatestamp.NO, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.fao.geonet.domain;

import org.fao.geonet.entitylistener.MetadataIdentifierTemplateListenerManager;

import javax.persistence.*;

/**
* An entity representing metadata identifier template. Metadata identifier templates are used
* to allow users to define custom metadata UUIDs following certain patters.
*
* @author Jose García
*/
@Entity
@Table(name = "MetadataIdentifierTemplate")
@Cacheable
@Access(AccessType.PROPERTY)
@EntityListeners(MetadataIdentifierTemplateListenerManager.class)
@SequenceGenerator(name= MetadataIdentifierTemplate.ID_SEQ_NAME, initialValue=100, allocationSize=1)
public class MetadataIdentifierTemplate extends GeonetEntity {
static final String ID_SEQ_NAME = "metadata_identifier_template_id_seq";

private int _id;
private String _name;
private String _template;
private char _systemProvided = 'n';

/**
* Get the id of the metadata identifier template.
* <p>
* This is autogenerated and when a new metadata identifier template is created
* the metadata identifier template will be assigned a new value.
* </p>
*
* @return the id of the metadata identifier template.
*/
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_SEQ_NAME)
@Column(nullable = false)
public int getId() {
return _id;
}

/**
* Get the id of the metadata identifier template.
* <p>
* This is autogenerated and when a new metadata identifier template is created
* the metadata identifier template will be assigned a new value.
* </p>
* <p>
* If you want to update an existing MetadataUrnTemplate then you should set this id
* to the metadata identifier template you want to update and set the other values to the desired values
* </p>
*
* @param id the id of the metadata identifier template.
* @return this metadata identifier template object
*/
public MetadataIdentifierTemplate setId(int id) {
this._id = id;
return this;
}

/**
* Get the basic/default name of the metadata identifier template. This is non-translated and
* can be used to look up the metadata identifier template like an id can.
* <p>
* This is a required property.
* <p>
* There is a max length to the name allowed. See the annotation for the length value
* </p>
*
* @return metadata identifier template name
*/
@Column(nullable = false, length = 32)
public String getName() {
return _name;
}

/**
* Set the basic/default name of the metadata identifier template. This is non-translated and
* can be used to look up the metadata identifier template like an id can.
* <p>
* This is a required property.
* <p>
* There is a max length to the name allowed. See the annotation on {@link #getName()} for the length value
* </p>
*
* @param name
* @return
*/
public MetadataIdentifierTemplate setName(String name) {
this._name = name;
return this;
}

/**
* Get a template value of the metadata identifier template.
*
* @return the description.
*/
@Column(nullable = false, length = 255)
public String getTemplate() {
return _template;
}

/**
* Set the template value.
*
* @param template the template value.
* @return this metadata identifier template object.
*/
public MetadataIdentifierTemplate setTemplate(String template) {
this._template = template;
return this;
}

/**
* Return true if the metadata identifier template is provided by GeoNetwork.
*
* @return true if the metadata identifier template is provided by GeoNetwork.
*/
@Transient
public boolean isSystemProvided() {
return Constants.toBoolean_fromYNChar(getSystemProvided_JPAWorkaround());
}

/**
* Set true if the metadata identifier template is provided by GeoNetwork.
*
* @param isSystemProvided true if the metadata identifier template is provided by GeoNetwork.
*/
public void setSystemDefault(boolean isSystemProvided) {
setSystemProvided_JPAWorkaround(Constants.toYN_EnabledChar(isSystemProvided));
}

/**
* For backwards compatibility we need the enabled column to be either 'n' or 'y'. This is a workaround to allow this until future
* versions of JPA that allow different ways of controlling how types are mapped to the database.
*/
@Column(name = "isprovided", length = 1, nullable = false, columnDefinition="Char(1) default 'n'")
protected char getSystemProvided_JPAWorkaround() {
return _systemProvided;
}

/**
* Set the enabled column value as either Constants.YN_ENABLED or Constants.YN_DISABLED
*
* @param _default either Constants.YN_ENABLED for true or Constants.YN_DISABLED for false
*/
protected void setSystemProvided_JPAWorkaround(char _default) {
this._systemProvided = _default;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.fao.geonet.entitylistener;

import org.fao.geonet.domain.MetadataIdentifierTemplate;

import javax.persistence.*;

/**
* @author Jose García
*/
public class MetadataIdentifierTemplateListenerManager extends AbstractEntityListenerManager<MetadataIdentifierTemplate> {
@PrePersist
public void prePresist(final MetadataIdentifierTemplate entity) {
handleEvent(PersistentEventType.PrePersist, entity);
}
@PreRemove
public void preRemove(final MetadataIdentifierTemplate entity) {
handleEvent(PersistentEventType.PreRemove, entity);
}
@PostPersist
public void postPersist(final MetadataIdentifierTemplate entity) {
handleEvent(PersistentEventType.PostPersist, entity);
}
@PostRemove
public void postRemove(final MetadataIdentifierTemplate entity) {
handleEvent(PersistentEventType.PostRemove, entity);
}
@PreUpdate
public void preUpdate(final MetadataIdentifierTemplate entity) {
handleEvent(PersistentEventType.PreUpdate, entity);
}
@PostUpdate
public void postUpdate(final MetadataIdentifierTemplate entity) {
handleEvent(PersistentEventType.PostUpdate, entity);
}
@PostLoad
public void postLoad(final MetadataIdentifierTemplate entity) {
handleEvent(PersistentEventType.PostLoad, entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.fao.geonet.repository;

import org.fao.geonet.domain.MetadataIdentifierTemplate;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Data Access object for the {@link MetadataIdentifierTemplate} entities.
*
* @author Jose García
*/
public interface MetadataIdentifierTemplateRepository extends
GeonetRepository<MetadataIdentifierTemplate, Integer>,
JpaSpecificationExecutor<MetadataIdentifierTemplate> {
/**
* Look up a metadata identifier template by its name.
*
* @param name the name of the metadata identifier template
*/
@Nullable
MetadataIdentifierTemplate findOneByName(@Nonnull String name);



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.fao.geonet.repository.specification;

import org.fao.geonet.domain.Constants;
import org.fao.geonet.domain.MetadataIdentifierTemplate;
import org.fao.geonet.domain.MetadataIdentifierTemplate_;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;

/**
* Specifications for querying MetadataIdentifierTemplate.
*
* @author Jose García
*/
public class MetadataIdentifierTemplateSpecs {
private MetadataIdentifierTemplateSpecs() {
// don't permit instantiation
}

public static Specification<MetadataIdentifierTemplate> isSystemProvided(final boolean isSystemProvided) {
return new Specification<MetadataIdentifierTemplate>() {
@Override
public Predicate toPredicate(Root<MetadataIdentifierTemplate> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

Path<Character> defaultAttributePath = root.get(MetadataIdentifierTemplate_.systemProvided_JPAWorkaround);
Predicate systemProvidedDefaultPredicate = cb.equal(defaultAttributePath, cb.literal(Constants.toYN_EnabledChar(isSystemProvided)));
return systemProvidedDefaultPredicate;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.fao.geonet.repository;

import org.fao.geonet.domain.MetadataIdentifierTemplate;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;

/**
* Tests for MetadataIdentifierTemplateRepository.
*
* @author Jose García
*/

public class MetadataIdentifierTemplateRepositoryTest extends AbstractSpringDataTest {

@Autowired
MetadataIdentifierTemplateRepository _repo;

@PersistenceContext
EntityManager _entityManager;

@Test
public void testFindOne() {
MetadataIdentifierTemplate mdUrnTemplate1 = newMetadataUrnTemplate();
mdUrnTemplate1 = _repo.save(mdUrnTemplate1);

MetadataIdentifierTemplate mdUrnTemplate2 = newMetadataUrnTemplate();
mdUrnTemplate2 = _repo.save(mdUrnTemplate2);

assertEquals(mdUrnTemplate2, _repo.findOne(mdUrnTemplate2.getId()));
assertEquals(mdUrnTemplate1, _repo.findOne(mdUrnTemplate1.getId()));
}

@Test
public void testFindOneByName() {
MetadataIdentifierTemplate mdUrnTemplate1 = newMetadataUrnTemplate();
mdUrnTemplate1 = _repo.save(mdUrnTemplate1);

MetadataIdentifierTemplate mdUrnTemplate2 = newMetadataUrnTemplate();
mdUrnTemplate2 = _repo.save(mdUrnTemplate2);

MetadataIdentifierTemplate metadataIdentifierTemplate = _repo.findOneByName(mdUrnTemplate1.getName());
assertEquals(mdUrnTemplate1.getName(), metadataIdentifierTemplate.getName());

metadataIdentifierTemplate = _repo.findOneByName(mdUrnTemplate2.getName());
assertEquals(mdUrnTemplate2.getName(), metadataIdentifierTemplate.getName());
}

private MetadataIdentifierTemplate newMetadataUrnTemplate() {
return newMetadataUrnTemplate(_inc);
}

public static MetadataIdentifierTemplate newMetadataUrnTemplate(AtomicInteger inc) {
int val = inc.incrementAndGet();
MetadataIdentifierTemplate mdUrnTemplate = new MetadataIdentifierTemplate();
mdUrnTemplate.setName("name" + val);
mdUrnTemplate.setTemplate("xxxx-{AA}-" + val);

return mdUrnTemplate;
}

}
Loading

0 comments on commit 6d788cf

Please sign in to comment.