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 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 void ensureValidSessionDate(int sessionDateYYYYMMDD) { DateUtils.ensureFormatYYYYMMDD(sessionDateYYYYMMDD); if ((sessionDateYYYYMMDD < startYYYYMMDD) || (sessionDateYYYYMMDD > endYYYYMMDD)) Utils.throwIncorrectSpecException( "Session date [" + sessionDateYYYYMMDD + "] should be between start [" + startYYYYMMDD + "] and end date [" + endYYYYMMDD + "] for Program [" + name + "]"); }
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(); }