Search in sources :

Example 1 with LocationTag

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);
}
Also used : LocationTag(org.openmrs.LocationTag) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Example 2 with LocationTag

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);
}
Also used : LocationTag(org.openmrs.LocationTag) APIException(org.openmrs.api.APIException)

Example 3 with LocationTag

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));
}
Also used : LocationTag(org.openmrs.LocationTag) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with LocationTag

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());
}
Also used : LocationTag(org.openmrs.LocationTag) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with LocationTag

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());
}
Also used : LocationTag(org.openmrs.LocationTag) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

LocationTag (org.openmrs.LocationTag)21 Test (org.junit.Test)17 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)16 Location (org.openmrs.Location)8 BindException (org.springframework.validation.BindException)2 Errors (org.springframework.validation.Errors)2 Criteria (org.hibernate.Criteria)1 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)1 Patient (org.openmrs.Patient)1 PatientIdentifierType (org.openmrs.PatientIdentifierType)1 Visit (org.openmrs.Visit)1 APIException (org.openmrs.api.APIException)1 LocationService (org.openmrs.api.LocationService)1 VisitService (org.openmrs.api.VisitService)1 AdtService (org.openmrs.module.emrapi.adt.AdtService)1 LocationDescriptor (org.openmrs.module.metadatadeploy.descriptor.LocationDescriptor)1 PaperRecord (org.openmrs.module.paperrecord.PaperRecord)1 PaperRecordServiceImpl (org.openmrs.module.paperrecord.PaperRecordServiceImpl)1 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1