Skip to content

Commit

Permalink
return map of wrong appointments at addAppointment
Browse files Browse the repository at this point in the history
according to the task it should return all nonPrivate appointments when a conflict happened
  • Loading branch information
carl committed Jun 15, 2011
1 parent 4e5c903 commit c20ebb2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion project/src/bean/Appointment.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Boolean conflictsWith(Appointment a)
{
assert(a != null);
// free is a special type and returns always true
if (a.type == AppointmentType.FREE || isPrivate)
if (a.type == AppointmentType.FREE)
return true;
return overlapsInTime(a);
}
Expand Down
33 changes: 21 additions & 12 deletions project/src/bean/CalendarMgt.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.GregorianCalendar;

import bean.IUserMgt;
Expand All @@ -26,8 +28,11 @@ public class CalendarMgt implements ICalendarMgt
@PersistenceContext
private EntityManager manager;

public Boolean addAppointment(GregorianCalendar start, GregorianCalendar end, String title, String notes, Boolean isPrivate, AppointmentType type, Collection<String> userEmails)
// returns a list of appointments which are in conflict or null
// throws exception if something strange happened (couldn't find UserMgt/remote)
public Map<String, Appointment> addAppointment(GregorianCalendar start, GregorianCalendar end, String title, String notes, Boolean isPrivate, AppointmentType type, Collection<String> userEmails) throws Exception
{
Map<String, Appointment> errorAppointments = new HashMap<String, Appointment>();
Appointment app = new Appointment();
app.start = start;
app.end = end;
Expand All @@ -37,20 +42,24 @@ public Boolean addAppointment(GregorianCalendar start, GregorianCalendar end, St
app.type = type;

IUserMgt uMgt = null;
try {
InitialContext ctx = new InitialContext();
uMgt = (IUserMgt) ctx.lookup("UserMgt/remote");
} catch (Exception e)
{
return false;
}
InitialContext ctx = new InitialContext();
uMgt = (IUserMgt) ctx.lookup("UserMgt/remote");

for (String email: userEmails)
{
Calendar cal = uMgt.getUserCalendar(email);
if (cal == null || cal.hasConflict(app))
return false;
{
if (app.isPrivate)
{
// cause we shouldn't send the user full information about a private appointment
app = new Appointment();
}
errorAppointments.put(email, app);
}
}
if (!errorAppointments.isEmpty())
return errorAppointments;
for (String email: userEmails)
{
Calendar cal = uMgt.getUserCalendar(email);
Expand All @@ -63,7 +72,7 @@ public Boolean addAppointment(GregorianCalendar start, GregorianCalendar end, St
*/
manager.merge(cal);
}
return true;
return null;
}

public List<Appointment> viewAppointments(String email)
Expand All @@ -81,7 +90,7 @@ public List<Appointment> viewAppointments(String email)
if (cal == null)
return null;
Collection<Appointment> appointments = cal.viewAppointments();
List sortedAppointments = new ArrayList(appointments);
List sortedAppointments = new LinkedList(appointments);
Collections.sort(sortedAppointments);
return sortedAppointments;
}
Expand Down
3 changes: 2 additions & 1 deletion project/src/bean/IAddAppointment.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package bean;

import java.util.Collection;
import java.util.Map;
import java.util.GregorianCalendar;

public interface IAddAppointment {
public Boolean addAppointment(GregorianCalendar start, GregorianCalendar end, String title, String notes, Boolean isPrivate, AppointmentType type, Collection<String> userEmails);
public Map<String, Appointment> addAppointment(GregorianCalendar start, GregorianCalendar end, String title, String notes, Boolean isPrivate, AppointmentType type, Collection<String> userEmails) throws Exception;
}
6 changes: 4 additions & 2 deletions project/src/bean/ICalendarMgt.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import bean.AppointmentType;
import java.util.GregorianCalendar;
import java.util.Collection;
import java.util.Map;
import java.util.List;

// our interfaces, which are visible for the client getting extended here - nevertheless redefine those methods here too cause they
// can also be used at other servercomponents
public interface ICalendarMgt extends IAddAppointment, IViewAppointment
{
public Boolean addAppointment(GregorianCalendar start, GregorianCalendar end, String title, String notes, Boolean isPrivate, AppointmentType type, Collection<String> userEmails);
public Collection<Appointment> viewAppointments(String email);
public Map<String, Appointment> addAppointment(GregorianCalendar start, GregorianCalendar end, String title, String notes, Boolean isPrivate, AppointmentType type, Collection<String> userEmails) throws Exception;
public List<Appointment> viewAppointments(String email);
}
6 changes: 3 additions & 3 deletions project/src/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.naming.InitialContext;
import java.util.HashSet;
import java.util.Collection;
import java.util.Map;
import java.util.GregorianCalendar;

public class Client
Expand All @@ -26,18 +27,17 @@ public static void main(String[] args) throws Exception
System.out.println("starting addAppointment");
HashSet <String> userEmails = new HashSet <String>();
userEmails.add("alice@a.com");
Boolean success = addApp.addAppointment(
Map<String, Appointment> errorAppointments = addApp.addAppointment(
new GregorianCalendar(2011, 6, 19, 12, 30),
new GregorianCalendar(2011, 6, 19, 13, 30),
"title",
"notes",
false, // isPrivate
AppointmentType.FREE,
userEmails);
if (!success)
if (errorAppointments != null)
System.out.println("Couldn't add appointment");


System.out.println("starting viewAppointments:");
viewAppointments(viewApp.viewAppointments("alice@a.com"));
}
Expand Down

0 comments on commit c20ebb2

Please sign in to comment.