Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Students have phones too #40

Merged
merged 10 commits into from
Aug 20, 2015
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.wisc.my.profile.model;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

Expand All @@ -18,6 +19,7 @@ public TypeValue(String type, String value) {

private String type;
private String value;
private DateTime lastModified;

public String getType() {
return type;
Expand All @@ -32,6 +34,14 @@ public void setValue(String value) {
this.value = value;
}

public DateTime getLastModified() {
return lastModified;
}

public void setLastModified(DateTime lastModified) {
this.lastModified = lastModified;
}

public boolean isEmpty() {
return StringUtils.isBlank(type) && StringUtils.isBlank(value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.wisc.my.profile.service;

import edu.wisc.my.profile.model.TypeValue;

public interface EmergencyPhoneNumberService{

/**
* Gets a user's emergency Phone Numbers
* @param netid the uid or net id of user
* @return a TypeValue array representing the phone numbers
*/
public TypeValue[] getEmergencyPhoneNumbers(String netid);

/**
* Sets the emergency phone number for a given user and returns the saved
* phone numbers
* @param phoneNumbers
* @return the phones number of that individual
* @throws Exception
*/
public TypeValue[] setEmergencyPhoneNumbers(String netid, TypeValue[] phoneNumbers) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import edu.wisc.my.profile.mapper.ContactInformationMapper;
import edu.wisc.my.profile.model.ContactInformation;
import edu.wisc.my.profile.model.TypeValue;

public class EmergencyContactMiddlewareDaoImpl implements EmergencyContactMiddlewareDao {

Expand All @@ -19,6 +20,9 @@ public class EmergencyContactMiddlewareDaoImpl implements EmergencyContactMiddle
@Autowired
private LocalContactMiddlewareDao lcdao;

@Autowired
private EmergencyPhoneNumberDao pNumDao;

public EmergencyContactMiddlewareDaoImpl(DataSource ds) {
jdbcTemplate = new JdbcTemplate(ds);
}
Expand All @@ -34,7 +38,8 @@ public ContactInformation[] setData(String netId, ContactInformation[] emergency
throws Exception {
//TODO: Fix this gross hack
ContactInformation localInfo = lcdao.getContactInfo(netId);
JSONObject json = ContactInformationMapper.convertToJSONObject(emergencyContacts, localInfo);
TypeValue[] phoneNumbers = pNumDao.getPhoneNumbers(netId);
JSONObject json = ContactInformationMapper.convertToJSONObject(emergencyContacts, localInfo, phoneNumbers);
json.put("NETID", netId);
if(logger.isTraceEnabled()) {
logger.trace("Saving the following JSON: " + json.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.wisc.my.profile.dao;

import edu.wisc.my.profile.model.TypeValue;

public interface EmergencyPhoneNumberDao {

/**
* Gets emergency phone numbers for user
* @param netid
* @return a TypeValue representing phone numbers
*/
TypeValue[] getPhoneNumbers(String netId);


/**
* Sets emergency phone numbers returning the saved phone numbers
* @param netid
* @param phoneNumbers
* @return the saved phone numbers
* @throws Exception
*/
TypeValue[] setPhoneNumbers(String netId, TypeValue[] phoneNumbers) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package edu.wisc.my.profile.dao;

import javax.sql.DataSource;

import org.joda.time.DateTime;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import edu.wisc.my.profile.mapper.ContactInformationMapper;
import edu.wisc.my.profile.model.ContactInformation;
import edu.wisc.my.profile.model.TypeValue;

public class EmergencyPhoneNumberDaoImpl implements EmergencyPhoneNumberDao {

protected final Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private EmergencyContactMiddlewareDao ecdao;

@Autowired
private LocalContactMiddlewareDao lcdao;

private JdbcTemplate jdbcTemplate;

public EmergencyPhoneNumberDaoImpl(DataSource ds) {
jdbcTemplate = new JdbcTemplate(ds);
}

@Override
public TypeValue[] getPhoneNumbers(String netId) {
TypeValue[] tv = jdbcTemplate.query("select PERSONPROFILE.PERSONPROFILE.GET_PERSON_PROFILE( ? ) from dual", new EmergencyPhoneNumberResultSetExtractor(), netId);
return tv;
}

@Override
public TypeValue[] setPhoneNumbers(String netId, TypeValue[] phoneNumbers) throws Exception {
// Keep the hack alive as in EmergencyContactMiddleWareDaoImpl and LocalContactMiddlewareDaoImpl
// Get the other info, add the new stuff and save the one object
ContactInformation[] emergencyContacts = ecdao.getData(netId);
ContactInformation localInfo = lcdao.getContactInfo(netId);
TypeValue[] phNumberWithDate = phoneNumbers;
for(TypeValue phoneNumber: phoneNumbers){
phoneNumber.setLastModified(DateTime.now());
}
JSONObject json = ContactInformationMapper.convertToJSONObject(emergencyContacts, localInfo, phNumberWithDate);
json.put("NETID", netId);
if(logger.isTraceEnabled()) {
logger.trace("Saving the following JSON: " + json.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slf4j enables a not-if-gated logger.trace("Saving the following JSON: {} .", json); at no meaningful performance penalty vs this syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look. This was copy and pasted, so I might look at our other uses of this.

}
String result = jdbcTemplate.query("select PERSONPROFILE.PERSONPROFILE.SAVE_PERSON_PROFILE( ? ) from dual", new MiddlewareUpdateExtractor(), json.toString());

if(MiddlewareUpdateExtractor.SUCCESS.equals(result)) {
//return saved content
return phoneNumbers;
} else {
throw new Exception("There was an issue saving the emergency contacts");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.wisc.my.profile.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import edu.wisc.my.profile.mapper.ContactInformationMapper;
import edu.wisc.my.profile.model.TypeValue;

public class EmergencyPhoneNumberResultSetExtractor implements ResultSetExtractor<TypeValue[]> {

protected final Logger logger = LoggerFactory.getLogger(getClass());

@Override
public TypeValue[] extractData(ResultSet rs) throws SQLException, DataAccessException {
if(rs.next()) {
String val = rs.getString(1);
TypeValue[] value = null;
try {
JSONObject json = new JSONObject(val);
value = ContactInformationMapper.convertToEmergencyPhoneNumber(json);
return value;
} catch (Exception e) {
logger.error("Issue fetching user info",e);
}
}
//error or empty
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import edu.wisc.my.profile.mapper.ContactInformationMapper;
import edu.wisc.my.profile.model.ContactInformation;
import edu.wisc.my.profile.model.TypeValue;

public class LocalContactMiddlewareDaoImpl implements LocalContactMiddlewareDao {

Expand All @@ -18,6 +19,9 @@ public class LocalContactMiddlewareDaoImpl implements LocalContactMiddlewareDao
@Autowired
private EmergencyContactMiddlewareDao ecdao;

@Autowired
private EmergencyPhoneNumberDao pNumDao;

private JdbcTemplate jdbcTemplate;

public LocalContactMiddlewareDaoImpl(DataSource ds) {
Expand All @@ -34,7 +38,8 @@ public ContactInformation getContactInfo(String netId) {
public ContactInformation setContactInfo(String netId, ContactInformation updatedContactInformation) throws Exception {
//hack until we refactor
ContactInformation[] emergencyContacts = ecdao.getData(netId);
JSONObject json = ContactInformationMapper.convertToJSONObject(emergencyContacts, updatedContactInformation);
TypeValue[] phoneNumbers = pNumDao.getPhoneNumbers(netId);
JSONObject json = ContactInformationMapper.convertToJSONObject(emergencyContacts, updatedContactInformation, phoneNumbers);
json.put("NETID", netId);
if(logger.isTraceEnabled()) {
logger.trace("Saving the following JSON: " + json.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.wisc.my.profile.mapper;


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

Expand Down Expand Up @@ -108,10 +107,40 @@ public static ContactInformation convertToLocalContactInformation(JSONObject jso
return ci;
}

public static JSONObject convertToJSONObject(ContactInformation[] emergencyContacts, ContactInformation ci) {
/**
* Extracts Phone Number from JSONObject
* @param json
* @return A TypeValue representing phone numbers, TypeValue may be empty if
* no phone number exists, may return null if parameter is null
*/
public static TypeValue[] convertToEmergencyPhoneNumber(JSONObject json){
if(json == null){
return null;
}
List<TypeValue> phoneNumbers = new ArrayList<TypeValue>();
//phoneNumbers
for(int i=1; i<=3; i++){
try {
JSONObject phoneNumberLine = json.getJSONObject("PHONE COUNT " + i);
if(phoneNumberLine != null){
TypeValue phoneNumber = new TypeValue();
phoneNumber.setValue(phoneNumberLine.getString("PHONE NUMBER"));
phoneNumber.setType(phoneNumberLine.getString("PHONE COMMENT"));
phoneNumbers.add(phoneNumber);
}
}catch(JSONException ex){
//Probably safe. Will throw exception if PHONE COUNT +i does not return object
logger.trace(ex.getMessage());
}
}
return phoneNumbers.toArray(new TypeValue[phoneNumbers.size()]);
}

public static JSONObject convertToJSONObject(ContactInformation[] emergencyContacts, ContactInformation ci, TypeValue[] phoneNumbers) {
JSONObject json = new JSONObject();
//populate local contact info
int count=1;
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-YY");
for(ContactAddress ca : ci.getAddresses()) {
JSONObject address = new JSONObject();
int alcount = 1;
Expand All @@ -134,13 +163,23 @@ public static JSONObject convertToJSONObject(ContactInformation[] emergencyConta
}

address.put("ADDRESS PRIORITY", count);
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-YY");
address.put("ADDRESS DTTM", formatter.print(ci.getLastModified()));
json.put("ADDRESS COUNT " + count++, address);
}

//TODO : populate local phone


//Add Emergency Phone Number
count = 1;
for(TypeValue phoneNumber: phoneNumbers){
if(phoneNumber.getType()!=null && phoneNumber.getValue()!=null){
JSONObject emergencyPhoneNumber = new JSONObject();
emergencyPhoneNumber.put("PHONE PRIORITY", count);
emergencyPhoneNumber.put("PHONE NUMBER", phoneNumber.getValue());
emergencyPhoneNumber.put("PHONE COMMENT", phoneNumber.getType());
emergencyPhoneNumber.put("PHONE DTTM", formatter.print(phoneNumber.getLastModified()));
json.put("PHONE COUNT "+count, emergencyPhoneNumber);
}
}

//populate emergency contact info
count = 1;
for(ContactInformation eci : emergencyContacts) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package edu.wisc.my.profile.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.wisc.my.profile.dao.EmergencyPhoneNumberDao;
import edu.wisc.my.profile.model.TypeValue;

@Service
public class EmergencyPhoneNumberServiceImpl implements EmergencyPhoneNumberService {

@Autowired
private EmergencyPhoneNumberDao dao;

@Override
public TypeValue[] getEmergencyPhoneNumbers(String netId) {
return dao.getPhoneNumbers(netId);
}

@Override
public TypeValue[] setEmergencyPhoneNumbers(String netId, TypeValue[] phoneNumbers) throws Exception{
return dao.setPhoneNumbers(netId, phoneNumbers);
}

}
Loading