Skip to content

Commit

Permalink
Merge pull request #7 from Bahmni-HWC/GOK-393
Browse files Browse the repository at this point in the history
Gok-393 | Modified Search Handler to filter Item by drugId
  • Loading branch information
riyaTw authored Oct 19, 2023
2 parents 116c8c1 + a625f51 commit d0d9a72
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@
*/
package org.openmrs.module.openhmis.inventory.api;

import org.openmrs.annotation.Authorized;
import org.openmrs.module.openhmis.commons.api.PagingInfo;
import org.openmrs.module.openhmis.commons.api.entity.IMetadataDataService;
import org.openmrs.module.openhmis.inventory.api.model.Item;
import org.openmrs.module.openhmis.inventory.api.model.ItemAttribute;
import org.openmrs.module.openhmis.inventory.api.model.ItemAttributeType;
import org.openmrs.module.openhmis.inventory.api.util.PrivilegeConstants;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* Interface that represents classes which perform data operations for {@link ItemAttribute}s.
*/
@Transactional
public interface IItemAttributeDataService extends IMetadataDataService<ItemAttribute> {}
public interface IItemAttributeDataService extends IMetadataDataService<ItemAttribute> {

@Transactional(readOnly = true)
@Authorized({ PrivilegeConstants.VIEW_ITEMS })
List<Item> getItemsByAttributeTypeAndValue(ItemAttributeType attributeType, String value,
boolean includeRetired, PagingInfo pagingInfo);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@
*/
package org.openmrs.module.openhmis.inventory.api.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.openmrs.module.openhmis.commons.api.PagingInfo;
import org.openmrs.module.openhmis.commons.api.entity.impl.BaseMetadataDataServiceImpl;
import org.openmrs.module.openhmis.commons.api.entity.security.IMetadataAuthorizationPrivileges;
import org.openmrs.module.openhmis.commons.api.f.Action1;
import org.openmrs.module.openhmis.inventory.api.IItemAttributeDataService;
import org.openmrs.module.openhmis.inventory.api.model.Item;
import org.openmrs.module.openhmis.inventory.api.model.ItemAttribute;
import org.openmrs.module.openhmis.inventory.api.model.ItemAttributeType;
import org.openmrs.module.openhmis.inventory.api.security.BasicMetadataAuthorizationPrivileges;
import org.openmrs.module.openhmis.inventory.api.util.HibernateCriteriaConstants;
import org.springframework.transaction.annotation.Transactional;
import org.hibernate.criterion.Order;

import java.util.ArrayList;
import java.util.List;

/**
* Data service implementation class for {@link ItemAttributeType}s.
*/
Expand All @@ -42,4 +51,46 @@ protected void validate(ItemAttribute entity) {
protected Order[] getDefaultSort() {
return new Order[] { Order.asc("id") };
}

public List<Item> getItemsByAttributeTypeAndValue(final ItemAttributeType attributeType,
final String value,
final boolean includeRetired,
PagingInfo pagingInfo) {

if (attributeType == null || value == null) {
throw new NullPointerException("The attributeType and value must be defined");
}

List<ItemAttribute> values = executeCriteria(ItemAttribute.class, pagingInfo, new Action1<Criteria>() {
@Override
public void apply(Criteria criteria) {
criteria.add(Restrictions.eq(HibernateCriteriaConstants.ATTRIBUTE_TYPE, attributeType));
criteria.add(Restrictions.eq("value", value));
if (!includeRetired) {
criteria.add(Restrictions.eq(HibernateCriteriaConstants.RETIRED, false));
}
}
}, getDefaultSort());

List<Item> items = new ArrayList<>();
if (values != null && !values.isEmpty()) {
for (ItemAttribute itemAttribute : values) {
if (itemAttribute.getOwner() != null) {
Item item = new Item();
item.setUuid(itemAttribute.getOwner().getUuid());
item.setName(itemAttribute.getOwner().getName());
item.setDescription(itemAttribute.getOwner().getDescription());
item.setRetired(itemAttribute.getOwner().getRetired());
item.setCodes(itemAttribute.getOwner().getCodes());
item.setDepartment(itemAttribute.getOwner().getDepartment());
item.setDefaultExpirationPeriod(itemAttribute.getOwner().getDefaultExpirationPeriod());
item.setHasPhysicalInventory(itemAttribute.getOwner().getHasPhysicalInventory());
item.setMinimumQuantity(itemAttribute.getOwner().getMinimumQuantity());
item.setDefaultPrice(itemAttribute.getOwner().getDefaultPrice());
items.add(item);
}
}
}
return items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ protected HibernateCriteriaConstants() {}
public static final String OPERATION_NUMBER = "operationNumber";
public static final String OPERATION_DATE = "operationDate";
public static final String OPERATION_ORDER = "operationOrder";
public static final String ATTRIBUTE_TYPE = "attributeType";

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.openmrs.module.openhmis.commons.api.entity.search.BaseObjectTemplateSearch;
import org.openmrs.module.openhmis.inventory.ModuleSettings;
import org.openmrs.module.openhmis.inventory.api.IDepartmentDataService;
import org.openmrs.module.openhmis.inventory.api.IItemAttributeDataService;
import org.openmrs.module.openhmis.inventory.api.IItemAttributeTypeDataService;
import org.openmrs.module.openhmis.inventory.api.IItemDataService;
import org.openmrs.module.openhmis.inventory.api.model.Department;
import org.openmrs.module.openhmis.inventory.api.model.Item;
Expand Down Expand Up @@ -49,24 +51,30 @@ public class ItemSearchHandler
Arrays.asList(
new SearchQuery.Builder(
"Find an item by its name or code, optionally filtering by department")
.withRequiredParameters("q")
.withRequiredParameters("drugId")
.withOptionalParameters("department_uuid", "has_physical_inventory")
.build()
)
);

private IItemDataService service;
private IDepartmentDataService departmentService;
private IItemAttributeDataService iItemAttributeDataService;
private IItemAttributeTypeDataService iItemAttributeTypeDataService;

@Autowired
public ItemSearchHandler(IItemDataService service, IDepartmentDataService departmentService) {
public ItemSearchHandler(IItemDataService service, IDepartmentDataService departmentService,
IItemAttributeDataService iItemAttributeDataService,
IItemAttributeTypeDataService iItemAttributeTypeDataService) {
this.service = service;
this.departmentService = departmentService;
this.iItemAttributeDataService = iItemAttributeDataService;
this.iItemAttributeTypeDataService = iItemAttributeTypeDataService;
}

@Override
public PageableResult search(RequestContext context) {
String query = context.getParameter("q");
String query = context.getParameter("drugId");
query = query.isEmpty() ? null : query;

String hasPhysicalInventoryString = context.getParameter("has_physical_inventory");
Expand All @@ -89,7 +97,9 @@ public PageableResult search(RequestContext context) {
// Check if the global wildcard search is enabled
if (ModuleSettings.useWildcardItemSearch()) {
query = '%' + query + '%';
items = service.getByNameFragment(query, context.getIncludeAll(), pagingInfo);
items =
iItemAttributeDataService.getItemsByAttributeTypeAndValue(
iItemAttributeTypeDataService.getById(1), query, context.getIncludeAll(), pagingInfo);
}

if (items == null || items.size() == 0) {
Expand All @@ -103,7 +113,9 @@ public PageableResult search(RequestContext context) {
//new paging info as otherwise the old one is used and paging does not work
pagingInfo = PagingUtil.getPagingInfoFromContext(context);
// If no items are found, search by name
items = service.getByNameFragment(query, context.getIncludeAll(), pagingInfo);
items =
iItemAttributeDataService.getItemsByAttributeTypeAndValue(iItemAttributeTypeDataService.getById(1),
query, context.getIncludeAll(), pagingInfo);
}
} else {
// Create the item search template with the specified parameters
Expand Down

0 comments on commit d0d9a72

Please sign in to comment.