Skip to content

Commit

Permalink
TRUNK-6007: When save person attribute type, only update search index…
Browse files Browse the repository at this point in the history
… if "searchable" property is saved
  • Loading branch information
mogoodrich committed Jun 1, 2021
1 parent 7120982 commit bae8500
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
19 changes: 15 additions & 4 deletions api/src/main/java/org/openmrs/api/db/PersonDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
*/
package org.openmrs.api.db;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.openmrs.Person;
import org.openmrs.PersonAddress;
import org.openmrs.PersonAttribute;
Expand All @@ -23,6 +19,10 @@
import org.openmrs.person.PersonMergeLog;
import org.openmrs.util.OpenmrsConstants;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* Person-related database functions
* <p>
Expand Down Expand Up @@ -199,6 +199,17 @@ public List<Relationship> getRelationships(Person fromPerson, Person toPerson, R
* <strong>Should</strong> get saved personAttributeType name from database
*/
public String getSavedPersonAttributeTypeName(PersonAttributeType personAttributeType);

/**
* Gets the value of the searchable property currently saved in the database for the given personAttributeType,
* bypassing any caches. This is used when saving an personAttributeType, so that we can
* determine if the searchable property has changed, which we use to determine whether we need to update the
* Lucene index
*
* @param personAttributeType
* @return the searchable property currently in the database for this personAttributeType
*/
public Boolean getSavedPersonAttributeTypeSearchable(PersonAttributeType personAttributeType);

/**
* @see org.openmrs.api.PersonService#getAllRelationshipTypes(boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,15 @@ public String getSavedPersonAttributeTypeName(PersonAttributeType personAttribut
sql.setInteger("personAttributeTypeId", personAttributeType.getId());
return (String) sql.uniqueResult();
}


@Override
public Boolean getSavedPersonAttributeTypeSearchable(PersonAttributeType personAttributeType) {
SQLQuery sql = sessionFactory.getCurrentSession().createSQLQuery(
"select searchable from person_attribute_type where person_attribute_type_id = :personAttributeTypeId");
sql.setInteger("personAttributeTypeId", personAttributeType.getId());
return (Boolean) sql.uniqueResult();
}

/**
* @see org.openmrs.api.db.PersonDAO#getPersonByUuid(java.lang.String)
*/
Expand Down
23 changes: 13 additions & 10 deletions api/src/main/java/org/openmrs/api/impl/PersonServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
*/
package org.openmrs.api.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.Person;
Expand Down Expand Up @@ -43,6 +36,13 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Default implementation of the PersonService
* <p>
Expand Down Expand Up @@ -182,9 +182,12 @@ public PersonAttributeType savePersonAttributeType(PersonAttributeType type) thr

PersonAttributeType attributeType = dao.savePersonAttributeType(type);

if (updateExisting) {
//we need to update index in case searchable property has changed
Context.updateSearchIndexForType(PersonAttribute.class);
if (updateExisting ) {
Boolean oldSearchable = dao.getSavedPersonAttributeTypeSearchable(type);
if (oldSearchable == null || !oldSearchable.equals(type.getSearchable())) {
//we need to update index searchable property has changed
Context.updateSearchIndexForType(PersonAttribute.class);
}
}

return attributeType;
Expand Down
17 changes: 13 additions & 4 deletions api/src/test/java/org/openmrs/api/db/PersonDAOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
*/
package org.openmrs.api.db;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openmrs.PersonAttributeType;
import org.openmrs.PersonName;
import org.openmrs.api.context.Context;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

public class PersonDAOTest extends BaseContextSensitiveTest {

private PersonDAO dao = null;
Expand Down Expand Up @@ -71,4 +71,13 @@ public void getPersonName_shouldNotGetPersonNameGivenInvalidId() {
assertNull(personName);
}

@Test
public void getSavedPersonAttributeTypeSearchable_shouldFetchSearchablePropertyForAPersonAttributeTypeBypassingCache(){
PersonAttributeType pat = dao.getPersonAttributeType(1);
pat.setSearchable(true);
// should still be false in the DB
Boolean searchable = dao.getSavedPersonAttributeTypeSearchable(pat);
assertFalse(searchable);
}

}

0 comments on commit bae8500

Please sign in to comment.