public static PracticeProp create(String client, String displayName, String login) { Client.ensureValid(client); User.ensureClientLevelPrivilege(client, login, ClientLevelPrivilege.UPDATE_PRACTICE); ensureNotNullNotEmpty(displayName, "displayName is null or empty"); String name = Utils.removeSpaceUnderscoreBracketAndHyphen(displayName.toLowerCase()); List<Key<PracticeEntity>> keys = ofy(client).load().type(PracticeEntity.class).filter("name", name).keys().list(); if (keys.size() != 0) throw new APIException() .status(Status.ERROR_RESOURCE_ALREADY_EXISTS) .message("There is already a practice with name [" + displayName + "]"); String key = getUniqueKey(client, name); long val = MemcacheServiceFactory.getMemcacheService().increment(key, 1, (long) 0); if (val != 1) throw new APIException() .status(Status.ERROR_RESOURCE_ALREADY_EXISTS) .message("There is already a group with name [" + displayName + "]"); PracticeEntity entity = new PracticeEntity(); entity.practiceId = Sequence.getNext(client, SequenceType.PRACTICE); entity.name = name; entity.displayName = displayName; ofy(client).save().entity(entity).now(); return entity.toProp(); }
public static PracticeEntity safeGetByIdOrName(String client, String idOrName) { Client.ensureValid(client); ensureNotNull(idOrName); if (Utils.canParseAsLong(idOrName)) { long practiceId = Utils.safeParseAsLong(idOrName); return safeGet(client, practiceId); } idOrName = Utils.removeSpaceUnderscoreBracketAndHyphen(idOrName.toLowerCase()); List<PracticeEntity> entities = ofy(client).load().type(PracticeEntity.class).filter("name", idOrName).list(); if (entities.size() == 0) throw new APIException() .status(Status.ERROR_RESOURCE_NOT_FOUND) .message("Practice [" + idOrName + "] does not exist"); if (entities.size() > 1) // should never come here throw new APIException() .status(Status.ERROR_RESOURCE_INCORRECT) .message( "Found [" + entities.size() + "] matches for practice [" + idOrName + "]. Please specify Id. "); return entities.get(0); }
public static Map<Long, PracticeEntity> getEntities(String client, Set<Long> practiceIds) { Client.ensureValid(client); Map<Long, PracticeEntity> map = ofy(client).load().type(PracticeEntity.class).ids(practiceIds); return map; }
public static void delete(String client, long practiceId, String login) { Client.ensureValid(client); User.ensureClientLevelPrivilege(client, login, ClientLevelPrivilege.UPDATE_PRACTICE); // there should not be any program type referencing this throw new APIException() .status(Status.ERROR_NOT_IMPLEMENTED) .message("This functionality is not implemented yet"); }
public static List<PracticeProp> getAll(String client) { Client.ensureValid(client); List<PracticeEntity> entities = ofy(client).load().type(PracticeEntity.class).order("name").list(); List<PracticeProp> props = new ArrayList<>(); for (PracticeEntity entity : entities) props.add(entity.toProp()); return props; }
public static PracticeEntity safeGet(String client, long practiceId) { Client.ensureValid(client); PracticeEntity entity = ofy(client).load().type(PracticeEntity.class).id(practiceId).now(); if (null == entity) throw new APIException() .status(Status.ERROR_RESOURCE_NOT_FOUND) .message("Practice [" + practiceId + "] does not exist"); return entity; }
public static Map<Long, PracticeProp> get(String client, Set<Long> practiceIds) { Client.ensureValid(client); Map<Long, PracticeEntity> map = ofy(client).load().type(PracticeEntity.class).ids(practiceIds); Map<Long, PracticeProp> props = new HashMap<>(); for (Long practiceId : map.keySet()) { props.put(practiceId, map.get(practiceId).toProp()); } return props; }
@Test public void getNextTest() { // client cannot be invalid try { SequenceCore.getNext("isha", "seq1", 5); assertTrue(false); } catch (APIException ex) { assertEquals(Status.ERROR_RESOURCE_NOT_FOUND, ex.statusCode); } Client.create("Isha"); // error if incorrect input try { SequenceCore.getNext("isha", "seq1", 0); assertTrue(false); } catch (APIException ex) { assertEquals(Status.ERROR_RESOURCE_INCORRECT, ex.statusCode); } // error if incorrect input try { SequenceCore.getNext("isha", "seq1", -1); assertTrue(false); } catch (APIException ex) { assertEquals(Status.ERROR_RESOURCE_INCORRECT, ex.statusCode); } List<Long> ids = SequenceCore.getNext("isha", "sEQ1", 5); assertEquals(5, ids.size()); assertEquals(1, ids.get(0).longValue()); assertEquals(2, ids.get(1).longValue()); assertEquals(3, ids.get(2).longValue()); assertEquals(4, ids.get(3).longValue()); assertEquals(5, ids.get(4).longValue()); // ensure it works for 1 ids = SequenceCore.getNext("isha", "seq1", 1); assertEquals(1, ids.size()); assertEquals(6, ids.get(0).longValue()); assertEquals(7, SequenceCore.getNext("isha", "seq1", 1).iterator().next().longValue()); // for some other sequence types ids = SequenceCore.getNext("isha", "seq2", 2); assertEquals(2, ids.size()); assertEquals(1, ids.get(0).longValue()); assertEquals(2, ids.get(1).longValue()); }
@Before public void setUp() { datastoreHelper.setUp(); ObjectifyFilter.complete(); Client.create(client); sgp = Group.create(client, "Singapore", User.SUPER_USER); assertEquals(1, sgp.groupId); kl = Group.create(client, "KL", User.SUPER_USER); assertEquals(2, kl.groupId); validUser = User.create(client, "*****@*****.**", sgp.groupId, User.SUPER_USER); userWithSendMailPermissionForSgp = User.create(client, "*****@*****.**", sgp.groupId, User.SUPER_USER); User.addGroupLevelPrivilege( client, sgp.groupId, userWithSendMailPermissionForSgp.email, GroupLevelPrivilege.SEND_EMAIL, User.SUPER_USER); sgpTeachers = List.createRestricted(client, sgp.groupId, "Sgp Teachers", null, User.SUPER_USER); ContactProp c = new ContactProp(); c.email = "*****@*****.**"; c.asOfyyyymmdd = 20141026; c.firstName = "Sathya"; sathya = Member.create(client, sgp.groupId, c, false, User.SUPER_USER); c.firstName = "Sharmila"; c.mobilePhone = "+6593849384"; c.email = null; sharmila = Member.create(client, sgp.groupId, c, false, User.SUPER_USER); assertEquals("Sharmila", sharmila.contact.firstName); c.email = "*****@*****.**"; murugavel = Member.create(client, sgp.groupId, c, false, User.SUPER_USER); assertEquals("*****@*****.**", murugavel.contact.email); // add sathya and sharmila to sgpTeachers Member.addOrDeleteList(client, sathya.memberId, sgpTeachers.listId, true, User.SUPER_USER); Member.addOrDeleteList(client, sharmila.memberId, sgpTeachers.listId, true, User.SUPER_USER); Member.subscribeGroup(client, sathya.memberId, sgp.groupId, User.SUPER_USER); Member.subscribeGroup(client, sharmila.memberId, sgp.groupId, User.SUPER_USER); ObjectifyFilter.complete(); }
public static PracticeProp rename( String client, long practiceId, String newDisplayName, String login) { Client.ensureValid(client); User.ensureClientLevelPrivilege(client, login, ClientLevelPrivilege.UPDATE_PRACTICE); PracticeEntity entity = safeGet(client, practiceId); ensureNotNullNotEmpty(newDisplayName, "newDisplayName is null or empty"); String newName = Utils.removeSpaceUnderscoreBracketAndHyphen(newDisplayName.toLowerCase()); if (entity.name.equals(newName)) { // ideally should be inside a transaction entity.displayName = newDisplayName; ofy(client).save().entity(entity).now(); return entity.toProp(); } List<Key<PracticeEntity>> keys = ofy(client).load().type(PracticeEntity.class).filter("name", newName).keys().list(); if (keys.size() != 0) throw new APIException() .status(Status.ERROR_RESOURCE_ALREADY_EXISTS) .message("There is already a practice with name [" + newDisplayName + "]"); String key = getUniqueKey(client, newName); long val = MemcacheServiceFactory.getMemcacheService().increment(key, 1, (long) 0); if (val != 1) throw new APIException() .status(Status.ERROR_RESOURCE_ALREADY_EXISTS) .message("There is already a practice with name [" + newDisplayName + "]"); // ideally should be inside a transaction entity.name = newName; entity.displayName = newDisplayName; ofy(client).save().entity(entity).now(); return entity.toProp(); }