net.lunikon.rethul.data.FileDAO.java Source code

Java tutorial

Introduction

Here is the source code for net.lunikon.rethul.data.FileDAO.java

Source

/*
 * Copyright (c) 2011 by Martin Simons.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package net.lunikon.rethul.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import net.lunikon.rethul.model.File;
import net.lunikon.rethul.model.FileStatus;
import net.lunikon.rethul.model.LanguageStatus;
import net.lunikon.rethul.model.Project;
import net.lunikon.rethul.model.ProjectLocale;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * FileDAO.
 * 
 * @author Martin Simons
 */
public class FileDAO extends AbstractDAO<File> {
    /**
     * The projetDao.
     */
    @Autowired
    private ProjectDAO projectDao;

    /**
     * Constructs a new instance of this class.
     */
    protected FileDAO() {
        super(File.class);
    }

    /**
     * Gets a file by the given name for the specified project.
     * 
     * @param project
     *            The project.
     * @param name
     *            The name.
     * @return the file or <code>null</code> if none exists.
     */
    public File getByProjectAndName(Project project, String name) {
        return (File) criteria() //
                .add(Restrictions.eq("project", project)) //
                .add(Restrictions.ilike("name", name)) //
                .setMaxResults(1) //
                .uniqueResult();
    }

    /**
     * Loads all files of the given project.
     * 
     * @param project
     *            The project.
     * @return a list of {@link File}s.
     */
    @SuppressWarnings("unchecked")
    public List<File> loadProjectFiles(Project project) {
        return criteria() //
                .add(Restrictions.eq("project", project)) //
                .addOrder(Order.asc("name")) //
                .list();
    }

    /**
     * Assembles a {@link FileStatus} object for the given {@link File}.
     * 
     * @param file
     *            The file to generate the status for.
     * @return the status file.
     */
    public FileStatus loadStatus(File file) {
        FileStatus status = new FileStatus(file);

        if (status.isMasterAvailable()) {
            // add all project languages by default
            List<LanguageStatus> stati = new ArrayList<LanguageStatus>();
            LanguageStatus master = new LanguageStatus(file, null);
            stati.add(master);
            List<ProjectLocale> locales = projectDao.getProjectLocales(file.getProject());
            for (ProjectLocale pl : locales)
                stati.add(new LanguageStatus(file, pl.getLocale()));

            // load counters for the languages
            @SuppressWarnings("unchecked")
            List<Object[]> raw = getSession() //
                    .createQuery(
                            "select locale, pending, count(*) from LocalizedString where file = :file group by locale, pending") //
                    .setParameter("file", file) //
                    .list();

            for (Object[] row : raw) {
                LanguageStatus ls = null;

                Locale locale = (Locale) row[0];
                if (locale == null)
                    ls = master;
                else {
                    for (int i = 0; i < stati.size(); i++) {
                        if (locale.equals(stati.get(i).getLocale())) {
                            ls = stati.get(i);
                            break;
                        }
                    }
                    if (ls == null)
                        continue; // not an active language anymore
                }

                boolean pending = (Boolean) row[1];
                int count = ((Number) row[2]).intValue();
                if (pending)
                    ls.setPending(count);
                else
                    ls.setDone(count);
            }
            status.setLanguages(stati);

            // determine missing entries
            for (LanguageStatus ls : stati) {
                if (ls.getLocale() == null)
                    continue;

                int missing = master.getDone() - ls.getPending() - ls.getDone();
                ls.setMissing(missing);
            }

            Collections.sort(stati);
        }

        return status;
    }
}