<commit_before>/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Web Questionnaires 2
 *
 * The Initial Owner of the Original Code is European Environment
 * Agency. Portions created by TripleDev are Copyright
 * (C) European Environment Agency.  All Rights Reserved.
 *
 * Contributor(s):
 *        <NAME>
 */
package eionet.webq.dao;

import eionet.webq.dao.orm.UserFile;
import eionet.webq.dto.UserFileIdUpdate;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;

import static org.hibernate.criterion.Restrictions.and;
import static org.hibernate.criterion.Restrictions.eq;
import static org.hibernate.criterion.Restrictions.in;

/**
 * {@link eionet.webq.dao.UserFileStorage} implementation.
 */
@Repository
public class UserFileStorageImpl extends AbstractDao<UserFile> implements UserFileDownload, UserFileStorage {

    @Override
    public int save(final UserFile file, final String userId) {
        file.setUserId(userId);
        getCurrentSession().save(file);
        return file.getId();
    }

    @Override
    public UserFile findFile(int id, String userId) {
        return (UserFile) getCriteria().add(and(eq("id", id), eq("userId", userId))).uniqueResult();
    }

    @Override
    public void update(final UserFile file, final String userId) {
        UserFile userFile = (UserFile) getCriteria().add(Restrictions.idEq(file.getId())).uniqueResult();
        getCurrentSession().evict(userFile);
        if (userId.equals(userFile.getUserId())) {
            file.setUpdated(new Timestamp(System.currentTimeMillis()));
            getCurrentSession().update(file);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Collection<UserFile> findAllUserFiles(String userId) {
        return getCriteria().add(eq("userId", userId)).list();
    }

    @Override
    public void remove(final String userId, final int... ids) {
        removeByCriterion(and(eq("userId", userId), in("id", ArrayUtils.toObject(ids))));
    }

    @Override
    public void updateUserId(UserFileIdUpdate updateData) {
        getCurrentSession().createQuery("UPDATE UserFile SET userId=:newId WHERE userId=:oldId AND userAgent=:userAgent")
                .setString("newId", updateData.getNewUserId())
                .setString("oldId", updateData.getOldUserId())
                .setString("userAgent", updateData.getUserAgent())
                .executeUpdate();
    }

    @Override
    public void updateDownloadTime(int userFileId) {
        getCurrentSession().createQuery("update UserFile set downloaded=:downloaded where id=:id")
                .setTimestamp("downloaded", new Date()).setInteger("id", userFileId).executeUpdate();
    }

    @Override
    Class<UserFile> getEntityClass() {
        return UserFile.class;
    }
}<commit_msg>log when user agent is null. Refs #17798<commit_after>/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Web Questionnaires 2
 *
 * The Initial Owner of the Original Code is European Environment
 * Agency. Portions created by TripleDev are Copyright
 * (C) European Environment Agency.  All Rights Reserved.
 *
 * Contributor(s):
 *        <NAME>jev
 */
package eionet.webq.dao;

import eionet.webq.dao.orm.UserFile;
import eionet.webq.dto.UserFileIdUpdate;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;

import static org.hibernate.criterion.Restrictions.and;
import static org.hibernate.criterion.Restrictions.eq;
import static org.hibernate.criterion.Restrictions.in;

/**
 * {@link eionet.webq.dao.UserFileStorage} implementation.
 */
@Repository
public class UserFileStorageImpl extends AbstractDao<UserFile> implements UserFileDownload, UserFileStorage {
    /**
     * This class logger.
     */
    private static final Logger LOGGER = Logger.getLogger(UserFileStorageImpl.class);

    @Override
    public int save(final UserFile file, final String userId) {
        file.setUserId(userId);
        getCurrentSession().save(file);
        return file.getId();
    }

    @Override
    public UserFile findFile(int id, String userId) {
        return (UserFile) getCriteria().add(and(eq("id", id), eq("userId", userId))).uniqueResult();
    }

    @Override
    public void update(final UserFile file, final String userId) {
        UserFile userFile = (UserFile) getCriteria().add(Restrictions.idEq(file.getId())).uniqueResult();
        getCurrentSession().evict(userFile);
        if (userId.equals(userFile.getUserId())) {
            file.setUpdated(new Timestamp(System.currentTimeMillis()));
            getCurrentSession().update(file);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Collection<UserFile> findAllUserFiles(String userId) {
        return getCriteria().add(eq("userId", userId)).list();
    }

    @Override
    public void remove(final String userId, final int... ids) {
        removeByCriterion(and(eq("userId", userId), in("id", ArrayUtils.toObject(ids))));
    }

    @Override
    public void updateUserId(UserFileIdUpdate updateData) {
        if (updateData.getUserAgent() != null) {
            getCurrentSession().createQuery("UPDATE UserFile SET userId=:newId WHERE userId=:oldId AND userAgent=:userAgent")
                    .setString("newId", updateData.getNewUserId()).setString("oldId", updateData.getOldUserId())
                    .setString("userAgent", updateData.getUserAgent()).executeUpdate();
        } else {
            LOGGER.warn("No user agent set in user file id update data.");
        }
    }

    @Override
    public void updateDownloadTime(int userFileId) {
        getCurrentSession().createQuery("update UserFile set downloaded=:downloaded where id=:id")
                .setTimestamp("downloaded", new Date()).setInteger("id", userFileId).executeUpdate();
    }

    @Override
    Class<UserFile> getEntityClass() {
        return UserFile.class;
    }
}
{'fraction_non_alphanumeric': 0.11017543859649123, 'fraction_numerical': 0.0019649122807017545, 'mean_word_length': 3.5738125802310656, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491788', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1984, 'n_words': 536}