<commit_before>/*
 *
 *  * Copyright 2014, Red Hat, Inc. and individual contributors as indicated by the
 *  * @author tags. See the copyright.txt file in the distribution for a full
 *  * listing of individual contributors.
 *  *
 *  * This is free software; you can redistribute it and/or modify it under the
 *  * terms of the GNU Lesser General Public License as published by the Free
 *  * Software Foundation; either version 2.1 of the License, or (at your option)
 *  * any later version.
 *  *
 *  * This software is distributed in the hope that it will be useful, but WITHOUT
 *  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 *  * details.
 *  *
 *  * You should have received a copy of the GNU Lesser General Public License
 *  * along with this software; if not, write to the Free Software Foundation,
 *  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 *  * site: http://www.fsf.org.
 */
package org.zanata.action;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.criterion.NaturalIdentifier;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.dao.ProjectDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.i18n.Messages;
import org.zanata.model.HLocale;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.seam.scope.ConversationScopeMessages;
import org.zanata.service.LocaleService;
import org.zanata.service.SlugEntityService;
import org.zanata.service.ValidationService;
import org.zanata.ui.autocomplete.LocaleAutocomplete;
import org.zanata.util.ComparatorUtil;
import org.zanata.webtrans.shared.model.ValidationAction;
import org.zanata.webtrans.shared.model.ValidationId;
import org.zanata.webtrans.shared.validation.ValidationFactory;

import javax.faces.application.FacesMessage;
import javax.faces.event.ValueChangeEvent;
import javax.persistence.EntityNotFoundException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Name("versionHome")
@Slf4j
public class VersionHome extends SlugHome<HProjectIteration> {

    private static final long serialVersionUID = 1L;

    public static final String PROJECT_ITERATION_UPDATE =
            "project.iteration.update";

    @Getter
    @Setter
    private String slug;

    @Getter
    @Setter
    private String projectSlug;

    @In
    private ProjectIterationDAO projectIterationDAO;

    @In
    private ConversationScopeMessages conversationScopeMessages;

    @In
    private LocaleService localeServiceImpl;

    @In
    private ValidationService validationServiceImpl;

    @In
    private SlugEntityService slugEntityServiceImpl;

    @In(create = true)
    private ProjectDAO projectDAO;

    @In
    private Messages msgs;

    @In
    private CopyVersionManager copyVersionManager;

    private Map<ValidationId, ValidationAction> availableValidations = Maps
            .newHashMap();

    @Getter
    @Setter
    private boolean isNewInstance = false;

    @Setter
    @Getter
    private String selectedProjectType;

    @Getter
    private VersionLocaleAutocomplete localeAutocomplete =
            new VersionLocaleAutocomplete();


    @Getter
    @Setter
    private boolean copyFromVersion = false;

    @Getter
    @Setter
    private String copyFromVersionSlug;

    private final Function<HProjectIteration, VersionItem> VERSION_ITEM_FN =
            new Function<HProjectIteration, VersionItem>() {
                @Override
                public VersionItem apply(HProjectIteration input) {
                    boolean selected = StringUtils.isNotEmpty(
                            copyFromVersionSlug) && copyFromVersionSlug
                            .equals(input.getSlug()) ? true : false;
                    return new VersionItem(selected, input);
                }
            };

    private void setDefaultCopyFromVersion() {
        List<VersionItem> otherVersions = getOtherVersions();
        if (!otherVersions.isEmpty()
                && StringUtils.isEmpty(copyFromVersionSlug)) {
            this.copyFromVersionSlug =
                    otherVersions.get(0).getVersion().getSlug();

        }
        copyFromVersion = true;
    }

    public void init(boolean isNewInstance) {
        this.isNewInstance = isNewInstance;
        if (isNewInstance) {
            ProjectType projectType = getProject().getDefaultProjectType();
            if (projectType != null) {
                selectedProjectType = projectType.name();
            }
            setDefaultCopyFromVersion();
        } else {
            ProjectType versionProjectType = getInstance().getProjectType();
            if (versionProjectType != null) {
                selectedProjectType = versionProjectType.name();
            }
            copyFromVersionSlug = "";
        }
    }

    public HProject getProject() {
        return projectDAO.getBySlug(projectSlug);
    }<commit_msg>Fix copy version checkbox: https://bugzilla.redhat.com/show_bug.cgi?id=1157358<commit_after>/*
 *
 *  * Copyright 2014, Red Hat, Inc. and individual contributors as indicated by the
 *  * @author tags. See the copyright.txt file in the distribution for a full
 *  * listing of individual contributors.
 *  *
 *  * This is free software; you can redistribute it and/or modify it under the
 *  * terms of the GNU Lesser General Public License as published by the Free
 *  * Software Foundation; either version 2.1 of the License, or (at your option)
 *  * any later version.
 *  *
 *  * This software is distributed in the hope that it will be useful, but WITHOUT
 *  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 *  * details.
 *  *
 *  * You should have received a copy of the GNU Lesser General Public License
 *  * along with this software; if not, write to the Free Software Foundation,
 *  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 *  * site: http://www.fsf.org.
 */
package org.zanata.action;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.criterion.NaturalIdentifier;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.dao.ProjectDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.i18n.Messages;
import org.zanata.model.HLocale;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.seam.scope.ConversationScopeMessages;
import org.zanata.service.LocaleService;
import org.zanata.service.SlugEntityService;
import org.zanata.service.ValidationService;
import org.zanata.ui.autocomplete.LocaleAutocomplete;
import org.zanata.util.ComparatorUtil;
import org.zanata.webtrans.shared.model.ValidationAction;
import org.zanata.webtrans.shared.model.ValidationId;
import org.zanata.webtrans.shared.validation.ValidationFactory;

import javax.faces.application.FacesMessage;
import javax.faces.event.ValueChangeEvent;
import javax.persistence.EntityNotFoundException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Name("versionHome")
@Slf4j
public class VersionHome extends SlugHome<HProjectIteration> {

    private static final long serialVersionUID = 1L;

    public static final String PROJECT_ITERATION_UPDATE =
            "project.iteration.update";

    @Getter
    @Setter
    private String slug;

    @Getter
    @Setter
    private String projectSlug;

    @In
    private ProjectIterationDAO projectIterationDAO;

    @In
    private ConversationScopeMessages conversationScopeMessages;

    @In
    private LocaleService localeServiceImpl;

    @In
    private ValidationService validationServiceImpl;

    @In
    private SlugEntityService slugEntityServiceImpl;

    @In(create = true)
    private ProjectDAO projectDAO;

    @In
    private Messages msgs;

    @In
    private CopyVersionManager copyVersionManager;

    private Map<ValidationId, ValidationAction> availableValidations = Maps
            .newHashMap();

    @Getter
    @Setter
    private boolean isNewInstance = false;

    @Setter
    @Getter
    private String selectedProjectType;

    @Getter
    private VersionLocaleAutocomplete localeAutocomplete =
            new VersionLocaleAutocomplete();


    @Getter
    @Setter
    private boolean copyFromVersion = true;

    @Getter
    @Setter
    private String copyFromVersionSlug;

    private final Function<HProjectIteration, VersionItem> VERSION_ITEM_FN =
            new Function<HProjectIteration, VersionItem>() {
                @Override
                public VersionItem apply(HProjectIteration input) {
                    boolean selected = StringUtils.isNotEmpty(
                            copyFromVersionSlug) && copyFromVersionSlug
                            .equals(input.getSlug()) ? true : false;
                    return new VersionItem(selected, input);
                }
            };

    private void setDefaultCopyFromVersion() {
        List<VersionItem> otherVersions = getOtherVersions();
        if (!otherVersions.isEmpty()
                && StringUtils.isEmpty(copyFromVersionSlug)) {
            this.copyFromVersionSlug =
                    otherVersions.get(0).getVersion().getSlug();

        }
    }

    public void init(boolean isNewInstance) {
        this.isNewInstance = isNewInstance;
        if (isNewInstance) {
            ProjectType projectType = getProject().getDefaultProjectType();
            if (projectType != null) {
                selectedProjectType = projectType.name();
            }
            if(StringUtils.isEmpty(copyFromVersionSlug)) {
                setDefaultCopyFromVersion();
            }
        } else {
            copyFromVersion = false;
            ProjectType versionProjectType = getInstance().getProjectType();
            if (versionProjectType != null) {
                selectedProjectType = versionProjectType.name();
            }
            copyFromVersionSlug = "";
        }
    }

    public HProject getProject() {
        return projectDAO.getBySlug(projectSlug);
    }
{'fraction_non_alphanumeric': 0.08232966276668961, 'fraction_numerical': 0.0047315898141775635, 'mean_word_length': 3.186172128195895, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '675972', 'n_tokens_mistral': 3339, 'n_tokens_neox': 3161, 'n_words': 817}