Search in sources :

Example 41 with Group

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

the class GroupDAO method getByIdList.

public List<Group> getByIdList(Set<Long> idsSet) {
    try {
        CriteriaQuery<Group> query = getBuilder().createQuery(Group.class);
        Root<Group> from = query.from(Group.class);
        query.where(from.get("id").in(idsSet));
        return currentSession().createQuery(query).list();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Group(org.jbei.ice.storage.model.Group) HibernateException(org.hibernate.HibernateException)

Example 42 with Group

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

the class MessageDAO method retrieveNewMessageCount.

public int retrieveNewMessageCount(Account account) {
    try {
        StringBuilder builder = new StringBuilder();
        builder.append("select count(id) from message m where m.is_read=false AND (m.id in ").append("(select message_id from message_destination_accounts where account_id = ").append(account.getId()).append(")");
        if (!account.getGroups().isEmpty()) {
            builder.append(" OR m.id in (select message_id from message_destination_groups where group_id in (");
            int i = 0;
            for (Group group : account.getGroups()) {
                if (i > 0)
                    builder.append(", ");
                builder.append(group.getId());
                i += 1;
            }
            builder.append("))");
        }
        builder.append(")");
        NativeQuery query = currentSession().createNativeQuery(builder.toString());
        Number number = (Number) query.uniqueResult();
        return number.intValue();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Group(org.jbei.ice.storage.model.Group) NativeQuery(org.hibernate.query.NativeQuery) HibernateException(org.hibernate.HibernateException)

Example 43 with Group

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

the class AccountController method authenticate.

/**
     * Authenticate a user in the database.
     * <p>
     * Using the {@link org.jbei.ice.lib.account.authentication.IAuthentication} specified in the
     * settings file, authenticate the user, and return the sessionData
     *
     * @param login
     * @param password
     * @param ip       IP Address of the user.
     * @return the account identifier (email) on a successful login, otherwise {@code null}
     */
protected Account authenticate(final String login, final String password, final String ip) {
    final IAuthentication authentication = new LocalAuthentication();
    String email;
    try {
        email = authentication.authenticates(login.trim(), password);
        if (email == null) {
            loginFailureCooldown();
            return null;
        }
    } catch (final AuthenticationException e2) {
        loginFailureCooldown();
        return null;
    }
    Account account = dao.getByEmail(email);
    if (account == null)
        return null;
    AccountPreferences accountPreferences = accountPreferencesDAO.getAccountPreferences(account);
    if (accountPreferences == null) {
        accountPreferences = new AccountPreferences();
        accountPreferences.setAccount(account);
        accountPreferencesDAO.create(accountPreferences);
    }
    // add to public groups
    List<Group> groups = groupDAO.getGroupsBy(GroupType.PUBLIC, true);
    try {
        if (groups != null) {
            for (Group group : groups) {
                if (!account.getGroups().contains(group)) {
                    account.getGroups().add(group);
                }
            }
            dao.update(account);
        }
    } catch (Exception e) {
        Logger.error(e);
    }
    account.setIp(ip);
    account.setLastLoginTime(Calendar.getInstance().getTime());
    account = save(account);
    UserSessions.createNewSessionForUser(account.getEmail());
    return account;
}
Also used : Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) AuthenticationException(org.jbei.ice.lib.account.authentication.AuthenticationException) LocalAuthentication(org.jbei.ice.lib.account.authentication.LocalAuthentication) IAuthentication(org.jbei.ice.lib.account.authentication.IAuthentication) AuthenticationException(org.jbei.ice.lib.account.authentication.AuthenticationException) PermissionException(org.jbei.ice.lib.access.PermissionException) AccountPreferences(org.jbei.ice.storage.model.AccountPreferences)

Example 44 with Group

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

the class EntriesAsCSV method writeList.

/**
     * Iterate through list of entries and extract values
     *
     * @param userId identifier of user making request
     * @throws IOException on Exception write values to file
     */
private void writeList(String userId, EntryField... fields) throws IOException {
    // filter entries based on what the user is allowed to see if the user is not an admin
    Account account = this.accountDAO.getByEmail(userId);
    if (account.getType() != AccountType.ADMIN) {
        List<Group> accountGroups = new GroupController().getAllGroups(account);
        entries = permissionDAO.getCanReadEntries(account, accountGroups, entries);
    }
    if (entries == null) {
        Logger.warn("No entries to convert to csv format");
        return;
    }
    // write headers
    Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    File tmpFile = File.createTempFile("ice-", ".csv", tmpPath.toFile());
    csvPath = tmpFile.toPath();
    FileWriter fileWriter = new FileWriter(tmpFile);
    if (fields == null || fields.length == 0)
        fields = getEntryFields();
    String[] headers = getCSVHeaders(fields);
    Set<Long> sequenceSet = new HashSet<>();
    try (CSVWriter writer = new CSVWriter(fileWriter)) {
        writer.writeNext(headers);
        // write entry fields
        for (long entryId : entries) {
            Entry entry = dao.get(entryId);
            //  get contents and write data out
            String[] line = new String[fields.length + 3];
            line[0] = entry.getCreationTime().toString();
            line[1] = entry.getPartNumber();
            int i = 1;
            for (EntryField field : fields) {
                line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
                i += 1;
            }
            if (this.includeSequences && sequenceDAO.hasSequence(entryId)) {
                line[i + 1] = getSequenceName(entry);
                sequenceSet.add(entryId);
            } else {
                line[i + 1] = "";
            }
            writer.writeNext(line);
        }
    }
    writeZip(userId, sequenceSet);
}
Also used : Path(java.nio.file.Path) Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) GroupController(org.jbei.ice.lib.group.GroupController) CSVWriter(com.opencsv.CSVWriter) EntryField(org.jbei.ice.lib.dto.entry.EntryField) Entry(org.jbei.ice.storage.model.Entry) ZipEntry(java.util.zip.ZipEntry) HashSet(java.util.HashSet)

Example 45 with Group

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

the class Entries method getCollectionEntries.

protected List<Long> getCollectionEntries(String collection, boolean all, EntryType type) {
    if (collection == null || collection.isEmpty())
        return null;
    Account account = accountDAO.getByEmail(userId);
    List<Long> entries;
    switch(collection.toLowerCase()) {
        case "personal":
            if (all)
                type = null;
            entries = dao.getOwnerEntryIds(userId, type);
            break;
        case "shared":
            entries = dao.sharedWithUserEntryIds(account, account.getGroups());
            break;
        case "available":
            Group publicGroup = new GroupController().createOrRetrievePublicGroup();
            entries = dao.getVisibleEntryIds(account.getType() == AccountType.ADMIN, publicGroup);
            break;
        default:
            return null;
    }
    return entries;
}
Also used : Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) GroupController(org.jbei.ice.lib.group.GroupController)

Aggregations

Group (org.jbei.ice.storage.model.Group)50 Account (org.jbei.ice.storage.model.Account)24 UserGroup (org.jbei.ice.lib.dto.group.UserGroup)16 HibernateException (org.hibernate.HibernateException)14 DAOException (org.jbei.ice.storage.DAOException)14 GroupController (org.jbei.ice.lib.group.GroupController)10 ArrayList (java.util.ArrayList)7 Entry (org.jbei.ice.storage.model.Entry)7 HashSet (java.util.HashSet)6 PermissionException (org.jbei.ice.lib.access.PermissionException)6 PartData (org.jbei.ice.lib.dto.entry.PartData)4 Folder (org.jbei.ice.storage.model.Folder)4 RemoteClientModel (org.jbei.ice.storage.model.RemoteClientModel)4 NativeQuery (org.hibernate.query.NativeQuery)3 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)3 AccessPermission (org.jbei.ice.lib.dto.access.AccessPermission)3 Results (org.jbei.ice.lib.dto.common.Results)3 FolderDetails (org.jbei.ice.lib.dto.folder.FolderDetails)3 Message (org.jbei.ice.storage.model.Message)3 AccountController (org.jbei.ice.lib.account.AccountController)2