Skip to content
Permalink
Browse files

Added persistance layer using JPA annotations

  • Loading branch information...
rkorytkowski committed Jan 9, 2013
1 parent 40790f3 commit 43f8f82d2a93d9de7d89ee77964917e7dedafc54
@@ -13,34 +13,91 @@
*/
package org.openmrs.module.social;

import org.openmrs.User;

public class SocialStatus {

private User user;
private String status;

public SocialStatus() {
}
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

public SocialStatus(User user, String status) {
this.user = user;
this.status = status;
}

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

public String getStatus() {
return status;
}
import org.openmrs.BaseOpenmrsData;
import org.openmrs.User;

public void setStatus(String status) {
this.status = status;
}
}
@Entity
@Table(name = "social_social_status")
public class SocialStatus extends BaseOpenmrsData {

@Id
@GeneratedValue
@Column(name = "social_status_id")
private Integer socialStatusId;

@ManyToOne
@JoinColumn(name = "user")
private User user;

@Basic
@Column(name = "status", length = 255)
private String status;

public SocialStatus() {
}

public SocialStatus(User user, String status) {
this.user = user;
this.status = status;
}

@Override
public Integer getId() {
return socialStatusId;
}

/**
* @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
*/
@Override
public void setId(Integer id) {
this.socialStatusId = id;
}

public Integer getSocialStatusId() {
return socialStatusId;
}

public void setSocialStatusId(Integer socialStatusId) {
this.socialStatusId = socialStatusId;
}

/**
* @see org.openmrs.BaseOpenmrsObject#getUuid()
*/
@Basic
@Access(AccessType.PROPERTY)
@Column(name = "uuid", length = 38, unique = true)
@Override
public String getUuid() {
return super.getUuid();
}

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

}
@@ -0,0 +1,50 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.social.api.db;

import org.hibernate.SessionFactory;
import org.openmrs.module.social.SocialStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
* DAO for {@link SocialStatus}.
*/
@Repository
public class SocialStatusDao {

@Autowired
SessionFactory sessionFactory;

/**
* Saves social status.
*
* @param status
* @should save status
*/
public SocialStatus saveSocialStatus(SocialStatus status) {
sessionFactory.getCurrentSession().saveOrUpdate(status);
return status;
}

/**
* Gets social status.
*
* @param id
* @return
*/
public SocialStatus getSocialStatus(Integer id) {
return (SocialStatus) sessionFactory.getCurrentSession().load(SocialStatus.class, id);
}
}
@@ -21,6 +21,7 @@
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.social.SocialStatus;
import org.openmrs.module.social.api.SocialService;
import org.openmrs.module.social.api.db.SocialStatusDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

@@ -38,6 +39,18 @@
@Autowired
@Qualifier("userService")
private UserService userService;

@Autowired
private SocialStatusDao dao;

/**
* Allows to inject a dao for test purposes.
*
* @param dao
*/
public void setDao(SocialStatusDao dao) {
this.dao = dao;
}

public void setUserService(UserService userService) {
this.userService = userService;
@@ -48,6 +61,7 @@ public void postStatusUpdate(SocialStatus status) {
User user = status.getUser();
user.setUserProperty(USER_PROPERTY_SOCIAL_STATUS, status.getStatus());
userService.saveUser(user, null);
dao.saveSocialStatus(status);
}

@Override
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">

<changeSet author="rafa" id="social-201301091556">
<createTable tableName="social_social_status">
<column name="social_status_id" type="integer" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="uuid" type="varchar(38)">
<constraints nullable="false"/>
</column>
<column name="status" type="varchar(255)" />
<column name="user" type="integer"/>
</createTable>

<addForeignKeyConstraint constraintName="social_social_status_user_fk"
baseTableName="social_social_status" baseColumnNames="user" referencedTableName="users"
referencedColumnNames="user_id" />
</changeSet>

</databaseChangeLog>
@@ -18,6 +18,7 @@
import org.openmrs.User;
import org.openmrs.api.UserService;
import org.openmrs.module.social.SocialStatus;
import org.openmrs.module.social.api.db.SocialStatusDao;
import org.openmrs.module.social.api.impl.SocialServiceImpl;

import java.util.Arrays;
@@ -36,13 +37,19 @@
public class SocialServiceTest {

private SocialService service;
private SocialStatusDao dao;
private UserService userService;


@Before
public void setUp() {
userService = mock(UserService.class);
dao = mock(SocialStatusDao.class);

service = new SocialServiceImpl();
((SocialServiceImpl) service).setUserService(userService);
SocialServiceImpl serviceImpl = (SocialServiceImpl) service;
serviceImpl.setUserService(userService);
serviceImpl.setDao(dao);
}

@Test
@@ -55,6 +62,7 @@ public void shouldSetStatus() {
service.postStatusUpdate(status);

verify(userService).saveUser(myself, null);
verify(dao).saveSocialStatus(status);
}

@Test
@@ -0,0 +1,39 @@
package org.openmrs.module.social.api.db;


import static org.hamcrest.Matchers.is;

import org.junit.Assert;
import org.junit.Test;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.module.social.SocialStatus;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;

public class SocialDaoTest extends BaseModuleContextSensitiveTest {

@Autowired
SocialStatusDao dao;

/**
* @see SocialStatusDao#saveSocialStatus(SocialStatus)
* @verifies save status
*/
@Test
public void saveSocialStatus_shouldSaveStatus() throws Exception {
User user = Context.getAuthenticatedUser();

SocialStatus status = new SocialStatus(user , "some status");

dao.saveSocialStatus(status);

Context.flushSession();
Context.clearSession();

SocialStatus persistedStatus = dao.getSocialStatus(status.getId());
Assert.assertThat(persistedStatus.getUser().getId(), is(status.getUser().getId()));
Assert.assertThat(persistedStatus.getStatus(), is(status.getStatus()));
Assert.assertThat(persistedStatus.getUuid(), is(status.getUuid()));
}
}
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!--
From applicationContext-service.xml in openmrs-api
Needed to override and add hibernate mappings to the classpath since omod is not packaged yet
-->
<bean id="sessionFactory" class="org.openmrs.api.db.hibernate.HibernateSessionFactoryBean">
<property name="configLocations">
<list>
<value>classpath:hibernate.cfg.xml</value>
</list>
</property>
<property name="mappingJarLocations">
<ref bean="mappingJarResources" />
</property>
<property name="packagesToScan">
<list>
<value>org.openmrs.module.social</value>
</list>
</property>
<!-- default properties must be set in the hibernate.default.properties -->
</bean>

</beans>
@@ -35,8 +35,9 @@


<!-- Maps hibernate file's, if present -->
<mappingFiles>
</mappingFiles>
<packagesWithMappedClasses>
org.openmrs.module.social
</packagesWithMappedClasses>

<!-- Internationalization -->
<!-- All message codes should start with ${project.parent.artifactId}. -->
@@ -33,7 +33,7 @@
</modules>

<properties>
<openMRSVersion>1.9.1</openMRSVersion>
<openMRSVersion>1.9.2</openMRSVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

0 comments on commit 43f8f82

Please sign in to comment.
You can’t perform that action at this time.