Search in sources :

Example 1 with RemoteClientModel

use of org.jbei.ice.storage.model.RemoteClientModel in project ice by JBEI.

the class Groups method addGroup.

/**
 * Adds group to the list of groups for current user
 *
 * @param userGroup information about group to be added, including members (local and remote)
 * @return added group
 */
public UserGroup addGroup(UserGroup userGroup) {
    if (userGroup.getType() == null)
        userGroup.setType(GroupType.PRIVATE);
    if (userGroup.getType() == GroupType.PUBLIC && !accountController.isAdministrator(userId)) {
        String errMsg = "Non admin '" + userId + "' attempting to create public group";
        Logger.error(errMsg);
        throw new PermissionException(errMsg);
    }
    Account account = accountDAO.getByEmail(userId);
    Group group = new Group();
    group.setUuid(Utils.generateUUID());
    group.setLabel(userGroup.getLabel());
    group.setDescription(userGroup.getDescription() == null ? "" : userGroup.getDescription());
    group.setType(userGroup.getType());
    group.setOwner(account);
    group.setAutoJoin(userGroup.isAutoJoin());
    group.setCreationTime(new Date());
    group = dao.create(group);
    // add local members
    if (userGroup.getMembers() != null && !userGroup.getMembers().isEmpty()) {
        for (AccountTransfer accountTransfer : userGroup.getMembers()) {
            Account memberAccount = accountDAO.getByEmail(accountTransfer.getEmail());
            if (memberAccount == null)
                continue;
            group.getMembers().add(memberAccount);
            memberAccount.getGroups().add(group);
            accountDAO.update(memberAccount);
        }
    }
    // add remote members
    for (RemoteUser remoteUser : userGroup.getRemoteMembers()) {
        RegistryPartner partner = remoteUser.getPartner();
        if (partner == null)
            continue;
        RemotePartner remotePartner = remotePartnerDAO.get(partner.getId());
        if (remotePartner == null)
            continue;
        AccountTransfer accountTransfer = remoteUser.getUser();
        if (accountTransfer == null || StringUtils.isEmpty(accountTransfer.getEmail()))
            continue;
        String email = accountTransfer.getEmail();
        RemoteClientModel remoteClientModel = remoteClientModelDAO.getModel(email, remotePartner);
        if (remoteClientModel == null) {
            remoteClientModel = new RemoteClientModel();
            remoteClientModel.setEmail(email);
            remoteClientModel.setRemotePartner(remotePartner);
            remoteClientModel = remoteClientModelDAO.create(remoteClientModel);
        }
        remoteClientModel.getGroups().add(group);
        remoteClientModelDAO.update(remoteClientModel);
    }
    return group.toDataTransferObject();
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) UserGroup(org.jbei.ice.lib.dto.group.UserGroup) RemoteUser(org.jbei.ice.lib.dto.web.RemoteUser) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner) RemotePartner(org.jbei.ice.storage.model.RemotePartner) RemoteClientModel(org.jbei.ice.storage.model.RemoteClientModel) AccountTransfer(org.jbei.ice.lib.account.AccountTransfer)

Example 2 with RemoteClientModel

use of org.jbei.ice.storage.model.RemoteClientModel in project ice by JBEI.

the class RemoteClientModelDAO method getClientCount.

/**
 * Retrieves number of clients for the specified group
 *
 * @param group group whose clients are of interest
 * @return number of clients for group
 * @throws DAOException
 */
public int getClientCount(Group group) {
    try {
        CriteriaQuery<Long> query = getBuilder().createQuery(Long.class);
        Root<RemoteClientModel> from = query.from(RemoteClientModel.class);
        Join<RemoteClientModel, Group> groups = from.join("groups");
        query.select(getBuilder().countDistinct(from.get("id")));
        query.where(getBuilder().equal(groups.get("id"), group.getId()), getBuilder().isNotNull(from.get("email")));
        return currentSession().createQuery(query).uniqueResult().intValue();
    } catch (Exception he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Group(org.jbei.ice.storage.model.Group) RemoteClientModel(org.jbei.ice.storage.model.RemoteClientModel) DAOException(org.jbei.ice.storage.DAOException)

Example 3 with RemoteClientModel

use of org.jbei.ice.storage.model.RemoteClientModel in project ice by JBEI.

the class RemoteClientModelDAO method getModel.

public RemoteClientModel getModel(String email, RemotePartner remotePartner) {
    try {
        CriteriaQuery<RemoteClientModel> query = getBuilder().createQuery(RemoteClientModel.class);
        Root<RemoteClientModel> from = query.from(RemoteClientModel.class);
        query.where(getBuilder().equal(from.get("email"), email), getBuilder().equal(from.get("remotePartner"), remotePartner));
        return currentSession().createQuery(query).uniqueResult();
    } catch (Exception he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) RemoteClientModel(org.jbei.ice.storage.model.RemoteClientModel) DAOException(org.jbei.ice.storage.DAOException)

Example 4 with RemoteClientModel

use of org.jbei.ice.storage.model.RemoteClientModel in project ice by JBEI.

the class RemoteClientModelDAO method getClientsForGroup.

/**
 * Retrieves clients belonging to specified group
 *
 * @param group group whose members are to be retrieved
 * @return remote clients that have been added to the specified group
 */
public List<RemoteClientModel> getClientsForGroup(Group group) {
    try {
        CriteriaQuery<RemoteClientModel> query = getBuilder().createQuery(RemoteClientModel.class);
        Root<RemoteClientModel> from = query.from(RemoteClientModel.class);
        Join<RemoteClientModel, Group> groups = from.join("groups");
        query.where(getBuilder().equal(groups.get("id"), group.getId()), getBuilder().isNotNull(from.get("email")));
        return currentSession().createQuery(query).list();
    } catch (Exception he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Group(org.jbei.ice.storage.model.Group) RemoteClientModel(org.jbei.ice.storage.model.RemoteClientModel) DAOException(org.jbei.ice.storage.DAOException)

Example 5 with RemoteClientModel

use of org.jbei.ice.storage.model.RemoteClientModel in project ice by JBEI.

the class Groups method setGroupMembers.

protected void setGroupMembers(Group group, List<AccountTransfer> members, List<RemoteUser> remoteUsers) {
    // 
    // deal with local members
    // 
    Set<String> memberHash = new HashSet<>();
    for (Account member : group.getMembers()) {
        memberHash.add(member.getEmail());
    }
    for (AccountTransfer accountTransfer : members) {
        String email = accountTransfer.getEmail();
        // do not add if found in set
        if (memberHash.remove(email))
            continue;
        // not found so add
        Account memberToAdd = accountDAO.getByEmail(email);
        if (memberToAdd == null) {
            Logger.error("Could not find account " + email + " to add to group");
            continue;
        }
        group.getMembers().add(memberToAdd);
        memberToAdd.getGroups().add(group);
        accountDAO.update(memberToAdd);
    }
    // all emails remaining should be removed
    for (String memberEmail : memberHash) {
        Account memberAccount = accountDAO.getByEmail(memberEmail);
        if (memberAccount == null)
            continue;
        memberAccount.getGroups().remove(group);
        group.getMembers().remove(memberAccount);
        accountDAO.update(memberAccount);
    }
    // 
    // deal with remote users
    // 
    Map<String, RemoteClientModel> remoteMembers = new HashMap<>();
    // get remote members
    List<RemoteClientModel> clients = remoteClientModelDAO.getClientsForGroup(group);
    for (RemoteClientModel clientModel : clients) {
        // technically the same email can be at different instances so adding partner id
        String identifier = (clientModel.getEmail() + clientModel.getRemotePartner().getUrl()).toLowerCase();
        remoteMembers.put(identifier, clientModel);
    }
    for (RemoteUser remoteUser : remoteUsers) {
        String identifier = (remoteUser.getUser().getEmail() + remoteUser.getPartner().getUrl()).toLowerCase();
        // do not add if found in set (already exists)
        if (remoteMembers.remove(identifier) != null)
            continue;
        // add   // todo : duplicates line 147. move to separate method
        RemotePartner remotePartner = remotePartnerDAO.getByUrl(remoteUser.getPartner().getUrl());
        if (remotePartner == null)
            continue;
        AccountTransfer accountTransfer = remoteUser.getUser();
        if (accountTransfer == null || StringUtils.isEmpty(accountTransfer.getEmail()))
            continue;
        String email = accountTransfer.getEmail();
        RemoteClientModel remoteClientModel = remoteClientModelDAO.getModel(email, remotePartner);
        if (remoteClientModel == null) {
            remoteClientModel = new RemoteClientModel();
            remoteClientModel.setEmail(email);
            remoteClientModel.setRemotePartner(remotePartner);
            remoteClientModel = remoteClientModelDAO.create(remoteClientModel);
        }
        remoteClientModel.getGroups().add(group);
        remoteClientModelDAO.update(remoteClientModel);
    }
    // remove all remaining
    if (remoteMembers.isEmpty())
        return;
    for (String identifier : remoteMembers.keySet()) {
        RemoteClientModel remoteClientModel = remoteMembers.get(identifier);
        remoteClientModel.getGroups().remove(group);
        remoteClientModelDAO.update(remoteClientModel);
    }
}
Also used : Account(org.jbei.ice.storage.model.Account) RemoteUser(org.jbei.ice.lib.dto.web.RemoteUser) RemotePartner(org.jbei.ice.storage.model.RemotePartner) RemoteClientModel(org.jbei.ice.storage.model.RemoteClientModel) AccountTransfer(org.jbei.ice.lib.account.AccountTransfer)

Aggregations

RemoteClientModel (org.jbei.ice.storage.model.RemoteClientModel)6 Group (org.jbei.ice.storage.model.Group)4 DAOException (org.jbei.ice.storage.DAOException)3 Account (org.jbei.ice.storage.model.Account)3 PermissionException (org.jbei.ice.lib.access.PermissionException)2 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)2 UserGroup (org.jbei.ice.lib.dto.group.UserGroup)2 RemoteUser (org.jbei.ice.lib.dto.web.RemoteUser)2 RemotePartner (org.jbei.ice.storage.model.RemotePartner)2 RegistryPartner (org.jbei.ice.lib.dto.web.RegistryPartner)1