Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
# 760 | +Shibha | Adding API to get programs in order by regimen temp…
Browse files Browse the repository at this point in the history
…late configured, Added Regimen and RegimenCategory and corresponding Tables. Added Manage regimen template as a new right. Added apis for regimen get and save
  • Loading branch information
khuranabalvinder committed Jun 20, 2013
1 parent 8c870ed commit 3210f46
Show file tree
Hide file tree
Showing 34 changed files with 577 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Program extends BaseModel {
private String description;
private Boolean active;
private boolean templateConfigured;
private boolean regimenTemplateConfigured;
private boolean push;

public Program(Long id) {
Expand Down
15 changes: 15 additions & 0 deletions modules/core/src/main/java/org/openlmis/core/domain/Regimen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.openlmis.core.domain;

import lombok.Data;

@Data
public class Regimen extends BaseModel {

private String name;
private String code;
private Long programId;
private Boolean active;
private RegimenCategory category;
private Integer displayOrder;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.openlmis.core.domain;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class RegimenCategory extends BaseModel {

private String name;
private String code;
private Integer displayOrder;

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public enum Right {
CONVERT_TO_ORDER("Requisition - Convert to Order", TRUE, "Permission to convert requisitions to order"),
VIEW_ORDER("Requisition - View Orders", TRUE, "Permission to view orders"),
MANAGE_PROGRAM_PRODUCT("Admin - Manage Program Product", TRUE, "Permission to manage program products"),
MANAGE_DISTRIBUTION("Allocation - Manage", FALSE, "Permission to manage an distribution");
MANAGE_DISTRIBUTION("Allocation - Manage", FALSE, "Permission to manage an distribution"),
MANAGE_REGIMEN_TEMPLATE("Admin - Manage Regimen Template", TRUE, "Permission to manage a regimen template");

@Getter
private final String rightName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openlmis.core.repository;

import org.openlmis.core.domain.Regimen;
import org.openlmis.core.repository.mapper.RegimenMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RegimenRepository {

@Autowired
RegimenMapper mapper;

public void insert(Regimen regimen) {
mapper.insert(regimen);
}

public void update(Regimen regimen) {
mapper.update(regimen);
}

public List<Regimen> getByProgram(Long programId) {
return mapper.getByProgram(programId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ List<Program> getProgramsForUserByFacilityAndRights(@Param("facilityId") Long fa
@Select("SELECT * FROM programs ORDER BY templateConfigured DESC, name")
List<Program> getAll();



@Select("SELECT * FROM programs WHERE LOWER(code) = LOWER(#{code})")
Program getByCode(String code);

@Select("SELECT * FROM programs ORDER BY regimenTemplateConfigured DESC, name")
List<Program> getAllByRegimenTemplate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.openlmis.core.repository.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.openlmis.core.domain.Regimen;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RegimenMapper {


@Insert({"INSERT INTO regimens (code, name, active, programId, categoryId, displayOrder, createdBy) ",
"VALUES (#{code}, #{name}, #{active}, #{programId}, #{category.id}, #{displayOrder}, #{createdBy})"})
@Options(useGeneratedKeys = true)
public void insert(Regimen regimen);


@Select({"SELECT * FROM regimens r INNER JOIN regimen_categories rc ON r.categoryid = rc.id ",
"WHERE r.programId=#{programId} ORDER BY rc.displayOrder,r.displayOrder"})
List<Regimen> getByProgram(Long programId);

@Update("UPDATE regimens set code=#{code}, name=#{name}, active=#{active}, displayOrder = #{displayOrder}")
void update(Regimen regimen);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openlmis.core.service;

import org.openlmis.core.domain.Regimen;
import org.openlmis.core.repository.RegimenRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegimenService {

@Autowired
RegimenRepository repository;

public void save(List<Regimen> regimens) {
for (Regimen regimen : regimens) {
if (regimen.getId() != null) {
repository.update(regimen);
} else {
repository.insert(regimen);
}
}
}


public List<Regimen> getByProgram(Long programId) {
return repository.getByProgram(programId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ProgramBuilder {
public static final Property<Program, String> programCode = newProperty();
public static final Property<Program, Boolean> programStatus = newProperty();
public static final Property<Program, Boolean> templateStatus = newProperty();
public static final Property<Program, Boolean> regimenTemplateConfigured = newProperty();
public static final Property<Program, Boolean> push = newProperty();
public static Property<Program, Long> programId = newProperty();

Expand All @@ -35,6 +36,7 @@ public Program instantiate(PropertyLookup<Program> lookup) {
program.setActive(lookup.valueOf(programStatus, true));
program.setTemplateConfigured(lookup.valueOf(templateStatus, false));
program.setPush(lookup.valueOf(push, false));
program.setRegimenTemplateConfigured(lookup.valueOf(regimenTemplateConfigured,Boolean.FALSE));
return program;
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright © 2013 VillageReach. All Rights Reserved. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
*
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.openlmis.core.builder;

import com.natpryce.makeiteasy.Instantiator;
import com.natpryce.makeiteasy.Property;
import com.natpryce.makeiteasy.PropertyLookup;
import org.openlmis.core.domain.Regimen;
import org.openlmis.core.domain.RegimenCategory;

import static com.natpryce.makeiteasy.Property.newProperty;

public class RegimenBuilder {


public static final Property<Regimen, String> regimenCode = newProperty();
public static final Property<Regimen, String> regimenName = newProperty();
public static final Property<Regimen, Boolean> active = newProperty();
public static final Property<Regimen, Long> programId = newProperty();
public static final Property<Regimen, Integer> displayOrder = newProperty();
public static final Property<Regimen, RegimenCategory> category = newProperty();

private static final String REGIMEN_CODE = "3TC/AZT";
private static final String REGIMEN_NAME = "3TC/AZT + EFV";
public static final Instantiator<Regimen> defaultRegimen = new Instantiator<Regimen>() {
@Override
public Regimen instantiate(PropertyLookup<Regimen> lookup) {
Regimen regimen = new Regimen();
regimen.setCode(lookup.valueOf(regimenCode, REGIMEN_CODE));
regimen.setName(lookup.valueOf(regimenName, REGIMEN_NAME));
regimen.setActive(lookup.valueOf(active, true));
regimen.setProgramId(lookup.valueOf(programId, 1L));
RegimenCategory regimenCategory = new RegimenCategory("ADULTS", "Adults",1);
regimenCategory.setId(1l);
regimen.setCategory(lookup.valueOf(category, regimenCategory));
regimen.setDisplayOrder(lookup.valueOf(displayOrder, 1));
return regimen;
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.openlmis.core.repository;

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.openlmis.core.domain.Regimen;
import org.openlmis.core.repository.mapper.RegimenMapper;
import org.openlmis.db.categories.UnitTests;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.isNotNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


@Category(UnitTests.class)
@RunWith(MockitoJUnitRunner.class)
public class RegimenRepositoryTest {

@Mock
RegimenMapper mapper;

@InjectMocks
RegimenRepository repository;


@Test
public void shouldInsertARegimen() {
Regimen regimen = new Regimen();
repository.insert(regimen);
verify(mapper).insert(regimen);
}

@Test
public void shouldUpdateARegimen() {
Regimen regimen = new Regimen();
repository.update(regimen);
verify(mapper).update(regimen);
}

@Test
public void shouldGetRegimenByProgram() {
List<Regimen> expectedRegimens = new ArrayList<>();
Long programId = 1l;
when(mapper.getByProgram(programId)).thenReturn(expectedRegimens);

List<Regimen> regimenList = repository.getByProgram(programId);

assertThat(regimenList,is(expectedRegimens));
verify(mapper).getByProgram(programId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import static java.lang.Boolean.FALSE;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.openlmis.core.builder.FacilityBuilder.defaultFacility;
import static org.openlmis.core.builder.ProgramBuilder.*;
import static org.openlmis.core.builder.ProgramSupportedBuilder.*;
Expand Down Expand Up @@ -270,6 +268,15 @@ public void shouldGetProgramsForAUserByFacilityAndRights() throws Exception {
assertTrue(programs.contains(activeProgram));
}


@Test
public void shouldGetAllProgramsInOrderByRegimentTemplateConfiguredAndName() {
insertProgram(make(a(defaultProgram, with(regimenTemplateConfigured, true))));
List<Program> programs = programMapper.getAllByRegimenTemplate();
assertThat(programs.size(), is(6));
assertThat(programs.get(0).getCode(), is(ProgramBuilder.PROGRAM_CODE));
}

private SupervisoryNode insertSupervisoryNode(SupervisoryNode supervisoryNode) {
supervisoryNodeMapper.insert(supervisoryNode);
return supervisoryNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.openlmis.core.repository.mapper;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.openlmis.core.domain.Regimen;
import org.openlmis.core.domain.RegimenCategory;
import org.openlmis.db.categories.IntegrationTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.natpryce.makeiteasy.MakeItEasy.a;
import static com.natpryce.makeiteasy.MakeItEasy.make;
import static com.natpryce.makeiteasy.MakeItEasy.with;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.openlmis.core.builder.RegimenBuilder.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test-applicationContext-core.xml")
@TransactionConfiguration(defaultRollback = true, transactionManager = "openLmisTransactionManager")
@Transactional
@Category(IntegrationTests.class)
public class RegimenMapperIT {

@Autowired
RegimenMapper mapper;

Regimen regimen;

@Before
public void setUp() throws Exception {
regimen = make(a(defaultRegimen));
}

@Test
public void shouldInsertARegimen() throws Exception {

mapper.insert(regimen);

assertNotNull(regimen.getId());
}

@Test
public void shouldGetAllRegimenForAProgramOrderByCategoryAndDisplayOrder(){
Long progId = 1l;
Regimen adultRegimen1 = make(a(defaultRegimen, with(regimenCode,"CODE_1"), with(displayOrder, 1), with(programId, progId)));
mapper.insert(adultRegimen1);
Regimen adultRegimen2 = make(a(defaultRegimen, with(regimenCode,"CODE_2"), with(displayOrder, 2), with(programId, progId)));
mapper.insert(adultRegimen2);
RegimenCategory regimenCategory = new RegimenCategory("PAEDIATRICS", "Paediatrics", 2);
regimenCategory.setId(2l);
Regimen paediatricsRegimen1 = make(a(defaultRegimen, with(regimenCode,"CODE_4"), with(displayOrder, 1), with(category, regimenCategory), with(programId, progId)));
mapper.insert(paediatricsRegimen1);
Regimen paediatricsRegimen2 = make(a(defaultRegimen, with(regimenCode,"CODE_3"), with(displayOrder, 2), with(category, regimenCategory), with(programId, progId)));
mapper.insert(paediatricsRegimen2);


List<Regimen> regimens = mapper.getByProgram(progId);

assertThat(regimens.size(), is(4));
assertThat(regimens.get(0).getCode(), is("CODE_1"));
assertThat(regimens.get(1).getCode(), is("CODE_2"));
assertThat(regimens.get(2).getCode(), is("CODE_4"));
assertThat(regimens.get(3).getCode(), is("CODE_3"));
}

@Test
public void shouldUpdateARegimen(){
mapper.insert(regimen);
regimen.setName("Regimen Updated Name");
regimen.setCode("Regimen Updated Code");
regimen.setActive(true);
regimen.setDisplayOrder(2);

mapper.update(regimen);

List<Regimen> updatedRegimenList = mapper.getByProgram(regimen.getProgramId());
assertThat(updatedRegimenList.get(0).getName(),is("Regimen Updated Name"));
assertThat(updatedRegimenList.get(0).getCode(),is("Regimen Updated Code"));
assertThat(updatedRegimenList.get(0).getActive(),is(true));
assertThat(updatedRegimenList.get(0).getDisplayOrder(),is(2));
}

}
Loading

0 comments on commit 3210f46

Please sign in to comment.