-
-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1190 from josegar74/feature-custom-metadata-ident…
…ifiers #1189 Metadata Identifier Templates
- Loading branch information
Showing
28 changed files
with
935 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
domain/src/main/java/org/fao/geonet/domain/MetadataIdentifierTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...rc/main/java/org/fao/geonet/entitylistener/MetadataIdentifierTemplateListenerManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
domain/src/main/java/org/fao/geonet/repository/MetadataIdentifierTemplateRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...rc/main/java/org/fao/geonet/repository/specification/MetadataIdentifierTemplateSpecs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
domain/src/test/java/org/fao/geonet/repository/MetadataIdentifierTemplateRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.