use of org.openmrs.LocationTag in project openmrs-core by openmrs.
the class HibernateLocationDAO method getLocationTagByName.
/**
* @see org.openmrs.api.db.LocationDAO#getLocationTagByName(java.lang.String)
*/
@Override
@SuppressWarnings("unchecked")
public LocationTag getLocationTagByName(String tag) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(LocationTag.class).add(Restrictions.eq("name", tag));
List<LocationTag> tags = criteria.list();
if (null == tags || tags.isEmpty()) {
return null;
}
return tags.get(0);
}
use of org.openmrs.LocationTag in project openmrs-core by openmrs.
the class LocationServiceImpl method saveLocation.
/**
* @see org.openmrs.api.LocationService#saveLocation(org.openmrs.Location)
*/
@Override
public Location saveLocation(Location location) throws APIException {
if (location.getName() == null) {
throw new APIException("Location.name.required", (Object[]) null);
}
// Check for transient tags. If found, try to match by name and overwrite, otherwise throw exception.
if (location.getTags() != null) {
for (LocationTag tag : location.getTags()) {
// only check transient (aka non-precreated) location tags
if (tag.getLocationTagId() == null) {
if (!StringUtils.hasLength(tag.getName())) {
throw new APIException("Location.tag.name.required", (Object[]) null);
}
LocationTag existing = Context.getLocationService().getLocationTagByName(tag.getName());
if (existing != null) {
location.removeTag(tag);
location.addTag(existing);
} else {
throw new APIException("Location.cannot.add.transient.tags", (Object[]) null);
}
}
}
}
CustomDatatypeUtil.saveAttributesIfNecessary(location);
return dao.saveLocation(location);
}
use of org.openmrs.LocationTag in project openmrs-core by openmrs.
the class LocationServiceTest method saveLocationTag_shouldCreateLocationTagSuccessfully.
/**
* Test to make sure that a simple save to a new location tag gets persisted to the database
*
* @see LocationService#saveLocationTag(LocationTag)
*/
@Test
public void saveLocationTag_shouldCreateLocationTagSuccessfully() {
LocationTag tag = new LocationTag();
tag.setName("testing");
tag.setDescription("desc");
LocationService ls = Context.getLocationService();
ls.saveLocationTag(tag);
LocationTag newSavedTag = ls.getLocationTag(tag.getLocationTagId());
assertNotNull("The saved tag should have an id now", tag.getLocationTagId());
assertNotNull("We should get back a tag", newSavedTag);
assertTrue("The created tag needs to equal the pojo location", tag.equals(newSavedTag));
}
use of org.openmrs.LocationTag in project openmrs-core by openmrs.
the class LocationServiceTest method unretireLocationTag_shouldUnretireRetiredLocationTag.
/**
* @see LocationService#unretireLocationTag(LocationTag)
*/
@Test
public void unretireLocationTag_shouldUnretireRetiredLocationTag() {
LocationService ls = Context.getLocationService();
LocationTag tag = ls.getLocationTagByName("Test Retired Tag");
Assert.assertTrue(tag.getRetired());
LocationTag newTag = ls.unretireLocationTag(tag);
Assert.assertEquals(tag, newTag);
Assert.assertFalse(tag.getRetired());
Assert.assertNull(tag.getRetiredBy());
Assert.assertNull(tag.getRetireReason());
}
use of org.openmrs.LocationTag in project openmrs-core by openmrs.
the class LocationServiceTest method retireLocationTag_shouldRetireLocationTagWithGivenReason.
/**
* @see LocationService#retireLocationTag(LocationTag,String)
*/
@Test
public void retireLocationTag_shouldRetireLocationTagWithGivenReason() {
LocationService ls = Context.getLocationService();
LocationTag tag = ls.getLocationTag(1);
Assert.assertFalse(tag.getRetired());
String reason = "because i can";
LocationTag newTag = ls.retireLocationTag(tag, reason);
Assert.assertEquals(tag, newTag);
Assert.assertTrue(tag.getRetired());
Assert.assertEquals(reason, tag.getRetireReason());
}
Aggregations