Skip to content

Commit

Permalink
Feat: Add rest endpoints for cohortMember attribute & attributeType
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliouzbett committed Jun 10, 2021
1 parent 159e434 commit 818fc07
Show file tree
Hide file tree
Showing 24 changed files with 267 additions and 156 deletions.
5 changes: 0 additions & 5 deletions api/pom.xml
Expand Up @@ -16,11 +16,6 @@
</description>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<!--
Add other dependencies from parent's pom:
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/org/openmrs/module/cohort/CohortMember.java
@@ -1,6 +1,6 @@
package org.openmrs.module.cohort;

import org.openmrs.BaseOpenmrsData;
import org.openmrs.BaseCustomizableData;
import org.openmrs.Patient;

import javax.persistence.CascadeType;
Expand All @@ -16,7 +16,7 @@

@Entity
@Table(name = "cohort_member")
public class CohortMember extends BaseOpenmrsData {
public class CohortMember extends BaseCustomizableData<CohortMemberAttribute> {

private static final long serialVersionUID = 1L;

Expand Down
Expand Up @@ -9,10 +9,12 @@
*/
package org.openmrs.module.cohort;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.openmrs.BaseOpenmrsData;
import org.openmrs.attribute.Attribute;
import org.openmrs.attribute.BaseAttribute;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -26,25 +28,27 @@
@Entity
@Table(name = "cohort_member_attribute")
@EqualsAndHashCode(callSuper = true)
@Data
@Getter
@Setter
@ToString
public class CohortMemberAttribute extends BaseOpenmrsData {
public class CohortMemberAttribute extends BaseAttribute<CohortMemberAttributeType, CohortMember> implements Attribute<CohortMemberAttributeType, CohortMember> {

private static final long serialVersionUID = 2L;
private static final long serialVersionUID = 1L;

@Id
@Column(name = "cohort_member_attribute_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer cohortMemberAttributeId;

@Column(name = "value_reference")
private String Value_reference;

@ManyToOne
@JoinColumn(name = "cohort_member_id")
private CohortMember cohortMember;

private String value;

@ManyToOne
@JoinColumn(name = "cohort_member_attribute_type_id")
@JoinColumn(name = "attribute_type_id")
private CohortMemberAttributeType cohortMemberAttributeType;

@Override
Expand All @@ -56,4 +60,12 @@ public Integer getId() {
public void setId(Integer id) {
this.cohortMemberAttributeId = id;
}

public void setCohortMember(CohortMember cohortMember) {
this.setOwner(cohortMember);
}

public CohortMember getCohortMember() {
return this.getOwner();
}
}
Expand Up @@ -9,10 +9,12 @@
*/
package org.openmrs.module.cohort;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.openmrs.BaseOpenmrsData;
import org.openmrs.attribute.AttributeType;
import org.openmrs.attribute.BaseAttributeType;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -23,25 +25,19 @@

@Entity
@Table(name = "cohort_member_attribute_type")
@EqualsAndHashCode(callSuper = true)
@Data
@Getter
@Setter
@ToString
public class CohortMemberAttributeType extends BaseOpenmrsData {
@NoArgsConstructor
public class CohortMemberAttributeType extends BaseAttributeType<CohortMember> implements AttributeType<CohortMember>{

private static final long serialVersionUID = 3L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "cohort_member_attribute_type_id")
@Column(name = "attribute_type_id")
private Integer cohortMemberAttributeTypeId;

@Column(nullable = false)
private String name;

private String description;

private String format;

@Override
public Integer getId() {
return this.cohortMemberAttributeTypeId;
Expand Down
Expand Up @@ -24,6 +24,8 @@ public interface CohortMemberService extends OpenmrsService {

CohortMemberAttributeType saveCohortMemberAttributeType(CohortMemberAttributeType cohortMemberAttributeType);

CohortMemberAttributeType deleteCohortMemberAttributeType(CohortMemberAttributeType cohortMemberAttributeType, String voidReason);

void purgeCohortMemberAttributeType(CohortMemberAttributeType cohortMemberAttributeType);

CohortMemberAttribute getCohortMemberAttributeByUuid(@NotNull String uuid);
Expand Down
Expand Up @@ -13,11 +13,6 @@
*/
package org.openmrs.module.cohort.api;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.openmrs.Concept;
import org.openmrs.EncounterType;
import org.openmrs.Form;
Expand All @@ -38,6 +33,11 @@
import org.openmrs.module.cohort.CohortVisit;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* This service exposes module's core functionality. It is a Spring managed bean which is configured in moduleApplicationContext.xml.
*
Expand Down
Expand Up @@ -28,7 +28,7 @@ public interface CohortMemberAttributeTypeDao {
CohortMemberAttributeType createCohortMemberAttributeType(@NotNull CohortMemberAttributeType cohortMemberAttributeType);

@Authorized(PrivilegeConstants.DELETE_COHORTS)
CohortMemberAttributeType deleteCohortMemberAttributeType (@NotNull String uuid);
CohortMemberAttributeType deleteCohortMemberAttributeType (@NotNull CohortMemberAttributeType cohortMemberAttributeType, String voidReason);

@Authorized(PrivilegeConstants.PURGE_COHORTS)
void purgeCohortMemberAttribute (CohortMemberAttributeType cohortMemberAttributeType);
Expand Down
Expand Up @@ -10,7 +10,9 @@
package org.openmrs.module.cohort.api.db.impl;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.openmrs.api.context.Context;
Expand All @@ -23,48 +25,49 @@
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@Component
@Setter(AccessLevel.PACKAGE)
@Getter(AccessLevel.PACKAGE)
public class CohortMemberAttributeTypeDaoImpl implements CohortMemberAttributeTypeDao {

@Autowired
@Setter(AccessLevel.PACKAGE)
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;

protected org.hibernate.Session getCurrentSession() {
protected org.hibernate.Session getSession() {
return sessionFactory.getCurrentSession();
}

@Override
public CohortMemberAttributeType getCohortMemberAttributeTypeByUuid(String uuid) {
return (CohortMemberAttributeType) getCurrentSession().createCriteria(CohortMemberAttributeType.class).add(
return (CohortMemberAttributeType) getSession().createCriteria(CohortMemberAttributeType.class).add(
Restrictions.eq("uuid", uuid)).uniqueResult();
}

@SuppressWarnings("unchecked")
@Override
public List<CohortMemberAttributeType> getCohortMemberAttributeTypes() {
return getCurrentSession().createCriteria(CohortMemberAttributeType.class).list();
return getSession().createCriteria(CohortMemberAttributeType.class).list();
}

@Override
public CohortMemberAttributeType createCohortMemberAttributeType(CohortMemberAttributeType cohortMemberAttributeType) {
getCurrentSession().saveOrUpdate(cohortMemberAttributeType);
getSession().saveOrUpdate(cohortMemberAttributeType);
return cohortMemberAttributeType;
}

@Override
public CohortMemberAttributeType deleteCohortMemberAttributeType(@NotNull String uuid) {
CohortMemberAttributeType cohortMemberAttributeType = getCohortMemberAttributeTypeByUuid(uuid);
cohortMemberAttributeType.setVoided(true);
cohortMemberAttributeType.setVoidReason("Voided via cohort rest call");
cohortMemberAttributeType.setVoidedBy(Context.getAuthenticatedUser());
getCurrentSession().saveOrUpdate(cohortMemberAttributeType);
public CohortMemberAttributeType deleteCohortMemberAttributeType(@NotNull CohortMemberAttributeType cohortMemberAttributeType, String voidReason) {
cohortMemberAttributeType.setRetired(true);
cohortMemberAttributeType.setRetireReason(voidReason);
cohortMemberAttributeType.setRetiredBy(Context.getAuthenticatedUser());
getSession().saveOrUpdate(cohortMemberAttributeType);
return cohortMemberAttributeType;
}

@Override
public void purgeCohortMemberAttribute(CohortMemberAttributeType cohortMemberAttributeType) {
getCurrentSession().delete(cohortMemberAttributeType);
getSession().delete(cohortMemberAttributeType);
}
}
Expand Up @@ -19,11 +19,13 @@
import org.openmrs.module.cohort.api.db.CohortMemberAttributeTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Component("cohortMemberService")
@Setter(AccessLevel.PACKAGE)
@Transactional
public class CohortMemberServiceImpl extends BaseOpenmrsService implements CohortMemberService {

@Autowired
Expand All @@ -43,10 +45,16 @@ public List<CohortMemberAttributeType> getAllCohortMemberAttributeTypes() {
}

@Override
@Transactional
public CohortMemberAttributeType saveCohortMemberAttributeType(CohortMemberAttributeType cohortMemberAttributeType) {
return attributeTypeDao.createCohortMemberAttributeType(cohortMemberAttributeType);
}

@Override
public CohortMemberAttributeType deleteCohortMemberAttributeType(CohortMemberAttributeType cohortMemberAttributeType, String voidReason) {
return attributeTypeDao.deleteCohortMemberAttributeType(cohortMemberAttributeType, voidReason);
}

@Override
public void purgeCohortMemberAttributeType(CohortMemberAttributeType cohortMemberAttributeType) {
attributeTypeDao.purgeCohortMemberAttribute(cohortMemberAttributeType);
Expand Down
49 changes: 32 additions & 17 deletions api/src/main/resources/liquibase.xml
Expand Up @@ -535,24 +535,28 @@
</createTable>
</changeSet>

<changeSet id="add_cohort_member_attribute_type_20210607" author="corneliouzbett">
<changeSet id="add_cohort_member_attribute_type_20210609" author="corneliouzbett">
<preConditions onFail="MARK_RAN" onError="WARN">
<not>
<tableExists tableName="cohort_member_attribute_type" />
</not>
</preConditions>
<createTable tableName="cohort_member_attribute_type">
<column name="cohort_member_attribute_type_id" autoIncrement="true" type="int(11)">
<column name="attribute_type_id" autoIncrement="true" type="int(11)">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="name" type="varchar(255)">
<constraints nullable="false" />
</column>
<column name="format" type="varchar(255)">
<constraints nullable="false" />
</column>

<column name="description" type="varchar(255)"/>
<column name="datatype" type="varchar(255)"/>
<column name="datatype_config" type="text"/>
<column name="preferred_handler" type="varchar(255)"/>
<column name="handler_config" type="text"/>
<column name="max_occurs" type="int(11)"/>
<column name="min_occurs" type="int(11)">
<constraints nullable="false"/>
</column>
<column name="date_created" type="datetime">
<constraints nullable="false" />
</column>
Expand All @@ -563,15 +567,15 @@
<constraints foreignKeyName="cohort_member_attribute_type_changed_by_fk" references="users(user_id)"/>
</column>
<column name="date_changed" type="datetime" />
<column name="voided" type="tinyint(1)" defaultValueBoolean="false">
<column name="retired" type="tinyint(1)" defaultValueBoolean="false">
<constraints nullable="false" />
</column>
<column name="voided_by" type="int(11)">
<column name="retired_by" type="int(11)">
<constraints foreignKeyName="cohort_member_attribute_type_voided_by_fk" references="users(user_id)"/>
</column>
<column name="date_voided" type="datetime" />
<column name="void_reason" type="varchar(255)" />
<column name="uuid" type="varchar(255)">
<column name="date_retired" type="datetime" />
<column name="retire_reason" type="varchar(255)" />
<column name="uuid" type="char(38)">
<constraints nullable="false" />
</column>
</createTable>
Expand All @@ -588,13 +592,13 @@
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="cohort_member_id" type="int(11)">
<constraints nullable="false" foreignKeyName="cohort_member_attribute_cohort_member_fk" references="cohort(cohort_id)"/>
<constraints nullable="false" foreignKeyName="cohort_member_attribute_cohort_member_fk" references="cohort_member(cohort_member_id)"/>
</column>
<column name="value" type="varchar(255)">
<column name="value_reference" type="TEXT">
<constraints nullable="false" />
</column>
<column name="cohort_member_attribute_type_id" type="int(11)">
<constraints nullable="false" foreignKeyName="cohort_member_attribute_cohort_member_attribute_type_fk" references="cohort_attributes_type(cohort_attribute_type_id)"/>
<column name="attribute_type_id" type="int(11)">
<constraints nullable="false" foreignKeyName="cohort_member_attribute_type_fk" references="cohort_member_attribute_type(attribute_type_id)"/>
</column>

<column name="date_created" type="datetime">
Expand All @@ -615,9 +619,20 @@
</column>
<column name="date_voided" type="datetime" />
<column name="void_reason" type="varchar(255)" />
<column name="uuid" type="varchar(255)">
<column name="uuid" type="char(38)">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
</databaseChangeLog>
<changeSet id="add_cohort_role_id_to_cohort_member_20210610" author="corneliouzbett">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="cohort_member" columnName="cohort_role_id" />
</not>
</preConditions>
<comment>Creating a cohort_role_id column</comment>
<addColumn tableName="cohort_member">
<column name="cohort_role_id" type="int(11)"/>
</addColumn>
</changeSet>
</databaseChangeLog>
Expand Up @@ -57,15 +57,15 @@ public static CohortMemberAttributeType COHORT_MEMBER_ATTRIBUTE_TYPE() {
cohortMemberAttributeType.setCohortMemberAttributeTypeId(103);
cohortMemberAttributeType.setName("cohort member attributeType Name");
cohortMemberAttributeType.setDescription("test cohort member attribute type");
cohortMemberAttributeType.setFormat("java.lang.String");
cohortMemberAttributeType.setDatatypeClassname("java.lang.String");
return cohortMemberAttributeType;
}

public static CohortMemberAttribute COHORT_MEMBER_ATTRIBUTE () {
CohortMemberAttribute cohortMemberAttribute = new CohortMemberAttribute();
cohortMemberAttribute.setId(1);
cohortMemberAttribute.setUuid("32816782-d578-401c-8475-8ccbb26ce001");
cohortMemberAttribute.setCohortMemberAttributeType(COHORT_MEMBER_ATTRIBUTE_TYPE());
cohortMemberAttribute.setAttributeType(COHORT_MEMBER_ATTRIBUTE_TYPE());
cohortMemberAttribute.setValue("cohortMemberAttribute");
cohortMemberAttribute.setCohortMemberAttributeId(100);
return cohortMemberAttribute;
Expand Down

0 comments on commit 818fc07

Please sign in to comment.