/** * Return the name of the user with the given internal DB id. * * If no user with that name exists, returns null. * @param profileId the id of the profile. * @return the name of the user, or null. */ public synchronized String getProfileUserName(int profileId) { try { UserProfile profile = (UserProfile) uosw.getObjectById(profileId, UserProfile.class); return profile.getUsername(); } catch (ObjectStoreException e) { return null; // Not in DB. } catch (NullPointerException e) { return null; // profile was null (impossible!) } }
/** * Return the name of the user with the given internal DB id. * * If no user with that name exists, returns null. * @param profileId the id of the profile. * @return the name of the user, or null. */ public synchronized String getProfileUserName(int profileId) { try { UserProfile profile = (UserProfile) uosw.getObjectById(profileId, UserProfile.class); return profile.getUsername(); } catch (ObjectStoreException e) { return null; // Not in DB. } catch (NullPointerException e) { return null; // profile was null (impossible!) } }
private Iterable<String> getAllSuperNames() throws ObjectStoreException { Set<String> names = new HashSet<String>(); UserProfile example = new UserProfile(); example.setSuperuser(true); for (UserProfile up: uosw.getObjectsByExample(example, singleton("superuser"))) { names.add(up.getUsername()); } return names; }
private Iterable<String> getAllSuperNames() throws ObjectStoreException { Set<String> names = new HashSet<String>(); UserProfile example = new UserProfile(); example.setSuperuser(true); for (UserProfile up: uosw.getObjectsByExample(example, singleton("superuser"))) { names.add(up.getUsername()); } return names; }
/** * Constructor with the actual objects we are trying to link. * @param bag The SavedBag we are trying to share * @param up The UserProfile we are trying to share this with. */ public UserAlreadyShareBagException(SavedBag bag, UserProfile up) { super(String.format("This bag (%s:%d) is already shared with this user (%s:%d)", bag.getName(), bag.getId(), up.getUsername(), up.getId())); } }
/** * Constructor with the actual objects we are trying to link. * @param bag The SavedBag we are trying to share * @param up The UserProfile we are trying to share this with. */ public UserAlreadyShareBagException(SavedBag bag, UserProfile up) { super(String.format("This bag (%s:%d) is already shared with this user (%s:%d)", bag.getName(), bag.getId(), up.getUsername(), up.getId())); } }
/** * Get a user's Profile using an ID * @param id userprofile ID * @return user's profile */ public Profile getProfile(int id) { Map<String, List<FieldDescriptor>> classKeys = getClassKeys(os.getModel()); UserProfile up; try { up = (UserProfile) uosw.getObjectById(id, UserProfile.class); } catch (ObjectStoreException e) { throw new RuntimeException("Error retrieving profile", e); } if (up != null && profileCache.containsKey(up.getUsername())) { return profileCache.get(up.getUsername()); } return wrapUserProfile(up, classKeys); }
/** * Get a user's Profile using an ID * @param id userprofile ID * @return user's profile */ public Profile getProfile(int id) { Map<String, List<FieldDescriptor>> classKeys = getClassKeys(os.getModel()); UserProfile up; try { up = (UserProfile) uosw.getObjectById(id, UserProfile.class); } catch (ObjectStoreException e) { throw new RuntimeException("Error retrieving profile", e); } if (up != null && profileCache.containsKey(up.getUsername())) { return profileCache.get(up.getUsername()); } return wrapUserProfile(up, classKeys); }
/** * Transform a tag into a string of some kind. * @param tag The input tag. Must not be null. * @return The output string. Might be null. */ public String transform(Tag tag) { if (tag == null) { throw new NullPointerException("tag must not be null."); } switch (field) { case Name: return tag.getTagName(); case ID: return tag.getObjectIdentifier(); case Type: return tag.getType(); case Owner: return tag.getUserProfile().getUsername(); default: throw new IllegalStateException("Unknown field: " + field); } }
/** * Transform a tag into a string of some kind. * @param tag The input tag. Must not be null. * @return The output string. Might be null. */ public String transform(Tag tag) { if (tag == null) { throw new NullPointerException("tag must not be null."); } switch (field) { case Name: return tag.getTagName(); case ID: return tag.getObjectIdentifier(); case Type: return tag.getType(); case Owner: return tag.getUserProfile().getUsername(); default: throw new IllegalStateException("Unknown field: " + field); } }
private Profile getProfileByApiKey(String token, Map<String, List<FieldDescriptor>> classKeys) { UserProfile profile = new UserProfile(); profile.setApiKey(token); Set<String> fieldNames = new HashSet<String>(); fieldNames.add("apiKey"); try { profile = uosw.getObjectByExample(profile, fieldNames); } catch (ObjectStoreException e) { return null; // Could not be found. } if (profile == null) { throw new AuthenticationException( "'" + token + "' is not a valid API access key"); } return getProfile(profile.getUsername(), classKeys); }
private Profile getProfileByApiKey(String token, Map<String, List<FieldDescriptor>> classKeys) { UserProfile profile = new UserProfile(); profile.setApiKey(token); Set<String> fieldNames = new HashSet<String>(); fieldNames.add("apiKey"); try { profile = uosw.getObjectByExample(profile, fieldNames); } catch (ObjectStoreException e) { return null; // Could not be found. } if (profile == null) { throw new AuthenticationException( "'" + token + "' is not a valid API access key"); } return getProfile(profile.getUsername(), classKeys); }
private boolean tagExists(List<Tag> tags, String name, String taggedObject, String type, String userName) { for (Tag tag : tags) { if (tag.getTagName().equals(name) && tag.getObjectIdentifier().equals(taggedObject) && tag.getType().equals(type) && tag.getUserProfile().getUsername().equals(userName)) { return true; } } return false; } }
private void storeShare(final InterMineBag bag, final UserProfile sharedWith) throws UserAlreadyShareBagException { final String userName = sharedWith.getUsername(); try { uosw.performUnsafeOperation(STORE_SHARE_SQL, new SQLOperation<Integer>() { @Override public Integer run(PreparedStatement stm) throws SQLException { stm.setInt(1, bag.getSavedBagId()); stm.setInt(2, sharedWith.getId()); return stm.executeUpdate(); } }); } catch (SQLException e) { throw new UserAlreadyShareBagException("Error sharing the " + " the bag : " + bag.getSavedBagId() + " with the user " + sharedWith.getId(), e); } informProfileOfChange(userName, new CreationEvent(bag)); }
private void storeShare(final InterMineBag bag, final UserProfile sharedWith) throws UserAlreadyShareBagException { final String userName = sharedWith.getUsername(); try { uosw.performUnsafeOperation(STORE_SHARE_SQL, new SQLOperation<Integer>() { @Override public Integer run(PreparedStatement stm) throws SQLException { stm.setInt(1, bag.getSavedBagId()); stm.setInt(2, sharedWith.getId()); return stm.executeUpdate(); } }); } catch (SQLException e) { throw new UserAlreadyShareBagException("Error sharing the " + " the bag : " + bag.getSavedBagId() + " with the user " + sharedWith.getId(), e); } informProfileOfChange(userName, new CreationEvent(bag)); }
/** * @param token permanent user token * @param classKeys class keys * @return permission */ public ApiPermission getPermission(PermanentToken token, Map<String, List<FieldDescriptor>> classKeys) { if (token.getUserProfile() == null) { // Remove it, as it is clearly invalid. removePermanentToken(token); throw new IllegalStateException("All permanent tokens should have users"); } Profile profile = getProfile(token.getUserProfile().getUsername(), classKeys); if (profile == null) { removePermanentToken(token); throw new AuthenticationException("This token is not a valid access key: " + token); } ApiPermission.Level level; try { level = ApiPermission.Level.valueOf(token.getLevel()); } catch (IllegalArgumentException e) { String badLevel = token.getLevel(); removePermanentToken(token); throw new IllegalStateException("Token has illegal level: " + badLevel); } return new ApiPermission(profile, level); }
/** * Delete all bags having as a type one of the classes deleted */ public void deleteBags() { Query q = new Query(); QueryClass qc = new QueryClass(SavedBag.class); q.addToSelect(qc); q.addFrom(qc); QueryField typeField = new QueryField(qc, "type"); BagConstraint constraint = new BagConstraint(typeField, ConstraintOp.IN, deletedClasses); q.setConstraint(constraint); Results bagsToDelete = uosw.execute(q, 1000, false, false, true); for (Iterator<?> i = bagsToDelete.iterator(); i.hasNext();) { ResultsRow<?> row = (ResultsRow<?>) i.next(); SavedBag savedBag = (SavedBag) row.get(0); Profile profile = pm.getProfile(savedBag.getUserProfile().getUsername()); try { profile.deleteBag(savedBag.getName()); System .out.println("Deleted the list: " + savedBag.getName() + " having type " + savedBag.getType()); } catch (ObjectStoreException ose) { System .out.println("Problems deleting bag: " + savedBag.getName()); } } }
public void testAddTag() { Tag createdTag = manager.addTag("wowTag", "list1", "bag", "bob"); Tag retrievedTag = manager.getTags("wowTag", "list1", "bag", "bob").get(0); assertEquals(createdTag, retrievedTag); assertEquals(createdTag.getTagName(), "wowTag"); assertEquals(createdTag.getObjectIdentifier(), "list1"); assertEquals(createdTag.getType(), "bag"); assertEquals(createdTag.getUserProfile().getUsername(), "bob"); }
public void testAddWithProfile() throws Exception { Tag createdTag = manager.addTag("wowTag", "list1", "bag", bobProfile); Tag retrievedTag = manager.getTags("wowTag", "list1", "bag", "bob").get(0); assertEquals(createdTag, retrievedTag); assertEquals(createdTag.getTagName(), "wowTag"); assertEquals(createdTag.getObjectIdentifier(), "list1"); assertEquals(createdTag.getType(), "bag"); assertEquals(createdTag.getUserProfile().getUsername(), "bob"); }
for (Tag tag : tagManager.getUserTags(userProfile.getUsername())) { tagManager.deleteTag(tag);