Modified calendar enteries to add extra fields.

This commit is contained in:
Dobie Wollert
2012-12-13 23:34:23 -08:00
parent c00cf48b1e
commit 859910faca
5 changed files with 139 additions and 79 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ war/*.log
*.class *.class
biomed.war biomed.war
gwt-unitCache/ gwt-unitCache/
.gwt/

File diff suppressed because one or more lines are too long

View File

@ -30,6 +30,7 @@ import com.google.common.collect.Maps;
public class CalendarApi { public class CalendarApi {
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory(); private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final String API_ACCOUNT = "api@atlanticbiomedical.com";
private static final String CLIENT_ID = "333768673996-904fuljpb428q9r37m2uujislhal5kt9.apps.googleusercontent.com"; private static final String CLIENT_ID = "333768673996-904fuljpb428q9r37m2uujislhal5kt9.apps.googleusercontent.com";
private static final String CLIENT_SECRET = "PAmHruqVbAXzEnnTwelx-Ll7"; private static final String CLIENT_SECRET = "PAmHruqVbAXzEnnTwelx-Ll7";
private static final String REFRESH_TOKEN = "1/fWvZebnDAhY0MlgK1IImcrGIDm4ZlILiRM8_47HsUFc"; private static final String REFRESH_TOKEN = "1/fWvZebnDAhY0MlgK1IImcrGIDm4ZlILiRM8_47HsUFc";
@ -66,8 +67,8 @@ public class CalendarApi {
Calendar calendar = buildCalendar(); Calendar calendar = buildCalendar();
try { try {
Event resultEvent = calendar.events() Event resultEvent = calendar.events().insert(API_ACCOUNT, event)
.insert("api@atlanticbiomedical.com", event).execute(); .execute();
return resultEvent.getId(); return resultEvent.getId();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -83,8 +84,7 @@ public class CalendarApi {
Calendar calendar = buildCalendar(); Calendar calendar = buildCalendar();
try { try {
Event event = calendar.events() Event event = calendar.events().get(API_ACCOUNT, eventId).execute();
.get("api@atlanticbiomedical.com", eventId).execute();
EventAttendee attendee = new EventAttendee(); EventAttendee attendee = new EventAttendee();
attendee.setEmail(email); attendee.setEmail(email);
@ -97,8 +97,7 @@ public class CalendarApi {
DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC")); DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end)); event.setEnd(new EventDateTime().setDateTime(end));
calendar.events().update("api@atlanticbiomedical.com", eventId, event) calendar.events().update(API_ACCOUNT, eventId, event).execute();
.execute();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -112,7 +111,7 @@ public class CalendarApi {
Calendar calendar = buildCalendar(); Calendar calendar = buildCalendar();
try { try {
calendar.events().delete("api@atlanticbiomedical.com", eventId).execute(); calendar.events().delete(API_ACCOUNT, eventId).execute();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -9,9 +9,10 @@ import com.biomed.shared.dispatch.DispatchService;
import com.biomed.shared.dispatch.DispatchResult; import com.biomed.shared.dispatch.DispatchResult;
import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
@Singleton @Singleton
public class DispatchServiceServlet extends RemoteServiceServlet implements DispatchService { public class DispatchServiceServlet extends RemoteServiceServlet implements DispatchService {
private final Dispatch dispatch; private final Dispatch dispatch;
@Inject @Inject
public DispatchServiceServlet(Dispatch dispatch) { public DispatchServiceServlet(Dispatch dispatch) {

View File

@ -16,97 +16,156 @@ import com.biomed.shared.api.EmptyResult;
import com.biomed.shared.api.ScheduleAction; import com.biomed.shared.api.ScheduleAction;
import com.google.inject.Provider; import com.google.inject.Provider;
public class ScheduleHandler implements ActionHandler<ScheduleAction, EmptyResult> { public class ScheduleHandler implements
ActionHandler<ScheduleAction, EmptyResult> {
private static final String QUERY = private static final String QUERY = "INSERT INTO workorder "
"INSERT INTO workorder " + + "(job_date, client_id, tech, job_start, job_end, reason, remarks, job_status_id, job_scheduled_date, caller)"
"(job_date, client_id, tech, job_start, job_end, reason, remarks, job_status_id, job_scheduled_date, caller)" + + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
private static final String FETCH_DATA_QUERY = private static final String FETCH_DATA_QUERY = "SELECT u.email, c.client_name, c.client_identification, c.address, c.address_2, c.city, c.state, c.zip, c.phone FROM user AS u, client AS c WHERE u.id = ? AND c.id = ?";
"SELECT u.email, c.client_name, c.address, c.city, c.state, c.zip FROM user AS u, client AS c WHERE u.id = ? AND c.id = ?";
private static final String UPDATE_EVENT_ID_QUERY = private static final String UPDATE_EVENT_ID_QUERY = "UPDATE workorder SET google_event_id = ? WHERE id = ?";
"UPDATE workorder SET google_event_id = ? WHERE id = ?";
@Override @Override
public Class<ScheduleAction> getType() { public Class<ScheduleAction> getType() {
return ScheduleAction.class; return ScheduleAction.class;
} }
@Inject @Inject
Provider<Connection> connectionProvider; Provider<Connection> connectionProvider;
@Inject @Inject
CalendarApi calendarApi; CalendarApi calendarApi;
@Override @Override
public EmptyResult execute(ScheduleAction action, ExecutionContext context) throws Exception { public EmptyResult execute(ScheduleAction action, ExecutionContext context)
Connection connection = connectionProvider.get(); throws Exception {
Connection connection = connectionProvider.get();
int workorderId = insertWorkorder(connection, action); int workorderId = insertWorkorder(connection, action);
String eventId = updateGoogleCalendar(connection, action); String eventId = updateGoogleCalendar(connection, action);
updateGoogleEventId(connection, workorderId, eventId); updateGoogleEventId(connection, workorderId, eventId);
connection.close(); connection.close();
return new EmptyResult(); return new EmptyResult();
} }
private static int insertWorkorder(Connection connection, ScheduleAction action) private static int insertWorkorder(Connection connection,
throws SQLException { ScheduleAction action) throws SQLException {
PreparedStatement query = connection.prepareStatement(QUERY,
Statement.RETURN_GENERATED_KEYS);
query.setString(1, SqlUtil.formatDate(action.getJobDate()));
query.setInt(2, action.getClientId());
query.setInt(3, action.getTechId());
query.setInt(4, action.getStartTime().toInteger());
query.setInt(5, action.getEndTime().toInteger());
query.setInt(6, action.getReason().getId());
query.setString(7, action.getRemarks());
query.setInt(8, action.getStatus().getId());
query.setString(9, SqlUtil.formatDate(new Date()));
query.setString(10, action.getRequestedBy());
query.execute();
PreparedStatement query = connection.prepareStatement(QUERY, Statement.RETURN_GENERATED_KEYS); ResultSet keys = query.getGeneratedKeys();
query.setString(1, SqlUtil.formatDate(action.getJobDate())); if (keys.next()) {
query.setInt(2, action.getClientId()); return keys.getInt(1);
query.setInt(3, action.getTechId()); } else {
query.setInt(4, action.getStartTime().toInteger()); return -1;
query.setInt(5, action.getEndTime().toInteger()); }
query.setInt(6, action.getReason().getId()); }
query.setString(7, action.getRemarks());
query.setInt(8, action.getStatus().getId());
query.setString(9, SqlUtil.formatDate(new Date()));
query.setString(10, action.getRequestedBy());
query.execute();
ResultSet keys = query.getGeneratedKeys(); private String updateGoogleCalendar(Connection connection,
if (keys.next()) { ScheduleAction action) throws SQLException {
return keys.getInt(1); Date startDate = action.getStartTime().toDate(action.getJobDate());
} else { Date endDate = action.getEndTime().toDate(action.getJobDate());
return -1;
}
}
private String updateGoogleCalendar(Connection connection, ScheduleAction action) PreparedStatement query2 = connection.prepareStatement(FETCH_DATA_QUERY);
throws SQLException { query2.setInt(1, action.getTechId());
Date startDate = action.getStartTime().toDate(action.getJobDate()); query2.setInt(2, action.getClientId());
Date endDate = action.getEndTime().toDate(action.getJobDate()); query2.execute();
PreparedStatement query2 = connection.prepareStatement(FETCH_DATA_QUERY); ResultSet results = query2.getResultSet();
query2.setInt(1, action.getTechId()); results.first();
query2.setInt(2, action.getClientId());
query2.execute();
ResultSet results = query2.getResultSet(); String email = results.getString("email");
results.first(); String summary = results.getString("client_name");
String location = formatLocation(results);
String description = formatDescription(results, action);
return calendarApi.scheduleEvent(email, startDate, endDate, summary,
description, location);
}
String email = results.getString("email"); private String formatLocation(ResultSet results) throws SQLException {
String summary = results.getString("client_name"); String address = results.getString("address");
String location = results.getString("address") + " " + results.getString("city") + " " + results.getString("city") + " " + results.getString("state") + " " + results.getString("zip"); String address2 = results.getString("address_2");
String description = action.getReason().getLabel() + "\n" + action.getRequestedBy() + "\n" + action.getRemarks() + "\n" + action.getStatus().getLabel(); String city = results.getString("city");
String state = results.getString("state");
String zip = results.getString("zip");
return calendarApi.scheduleEvent(email, startDate, endDate, summary, description, location); if (address2 != null) {
} return String.format("%s %s, %s, %s. %s", address, address2, city, state, zip);
} else {
return String.format("%s, %s, %s. %s", address, city, state, zip);
}
}
private String formatDescription(ResultSet results, ScheduleAction action) throws SQLException {
StringBuilder builder = new StringBuilder();
private static void updateGoogleEventId(Connection connection, int workorderId, String eventId) String address2 = results.getString("address_2");
throws SQLException {
builder
.append("Customer:\n")
.append("\t").append(results.getString("client_identification")).append("\n\n")
.append("Phone:\n")
.append("\t").append(results.getString("phone")).append("\n\n")
.append("Address:\n")
.append("\t").append(results.getString("address")).append("\n");
PreparedStatement query = connection.prepareStatement(UPDATE_EVENT_ID_QUERY); if (address2 != null) {
query.setString(1, eventId); builder.append("\t").append(address2).append("\n");
query.setInt(2, workorderId); }
builder
.append("\t").append(results.getString("city"))
.append(", ").append(results.getString("state"))
.append(". ").append(results.getString("zip"))
.append("\n\n");
if (action.getRequestedBy() != null) {
builder.append("Requested By:\n")
.append("\t").append(action.getRequestedBy()).append("\n\n");
}
query.execute(); builder
} .append("Reason:\n")
.append("\t").append(action.getReason().getLabel()).append("\n\n")
.append("Status:\n")
.append("\t").append(action.getStatus().getLabel()).append("\n\n");
if (action.getRemarks() != null) {
builder.append("Remarks:\n")
.append("\t").append(action.getRemarks()).append("\n");
}
return builder.toString();
}
private static void updateGoogleEventId(Connection connection,
int workorderId, String eventId) throws SQLException {
PreparedStatement query = connection
.prepareStatement(UPDATE_EVENT_ID_QUERY);
query.setString(1, eventId);
query.setInt(2, workorderId);
query.execute();
}
} }