/*
 * Copyright 2010, 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 java.io.Serializable;
import java.util.Map;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.international.StatusMessage;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.model.HCopyTransOptions;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.seam.scope.FlashScopeBean;

/**
 * Copy Trans page action bean.
 *
 * @author Carlos Munoz <a
 *         href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
 */
@Name("copyTransAction")
public class CopyTransAction implements Serializable {
    private static final long serialVersionUID = 1L;

    @In
    private ProjectIterationDAO projectIterationDAO;

    @In
    private CopyTransManager copyTransManager;

    @In
    private FlashScopeBean flashScope;

    @In
    private Map<String, String> messages;

    @In
    private CopyTransOptionsModel copyTransOptionsModel;

    private String iterationSlug;

    private String projectSlug;

    private HProjectIteration projectIteration;

    public String getIterationSlug() {
        return iterationSlug;
    }

    public void setIterationSlug(String iterationSlug) {
        this.iterationSlug = iterationSlug;
    }

    public String getProjectSlug() {
        return projectSlug;
    }

    public void setProjectSlug(String projectSlug) {
        this.projectSlug = projectSlug;
    }

    public HProjectIteration getProjectIteration() {
        if (this.projectIteration == null) {
            this.projectIteration =
                    projectIterationDAO.getBySlug(projectSlug, iterationSlug);
        }
        return this.projectIteration;
    }

    public void initialize() {
        HProject project = this.getProjectIteration().getProject();
        if (project.getDefaultCopyTransOpts() != null) {
            copyTransOptionsModel
                    .setInstance(project.getDefaultCopyTransOpts());
        }
    }

    public boolean isCopyTransRunning() {
        return copyTransManager.isCopyTransRunning(getProjectIteration());
    }

    @Restrict("#{s:hasPermission(copyTransAction.projectIteration, 'copy-trans')}")
    public
            void startCopyTrans() {
        if (isCopyTransRunning()) {
            StatusMessage statusMessage =
                    new StatusMessage(
                            StatusMessage.Severity.INFO,
                            null,
                            null,
                            messages.get("jsf.iteration.CopyTrans.AlreadyStarted.flash"),
                            null);
            flashScope.setAttribute("message", statusMessage);
            return;
        } else if (getProjectIteration().getDocuments().size() <= 0) {
            StatusMessage statusMessage =
                    new StatusMessage(
                            StatusMessage.Severity.INFO,
                            null,
                            null,
                            messages.get("jsf.iteration.CopyTrans.NoDocuments"),
                            null);
            flashScope.setAttribute("message", statusMessage);
            return;
        }

        // Options
        HCopyTransOptions options = copyTransOptionsModel.getInstance();

        copyTransManager.startCopyTrans(getProjectIteration(), options);
        flashScope.setAttribute("message",
                messages.get("jsf.iteration.CopyTrans.Started"));
    }

    public void cancel() {
        // Simply navigate where needed
    }
}

@@ -1031,30 +1031,8 @@
 ble;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -1418,16 +1418,55 @@
 opeBean;
+%0Aimport org.zanata.util.ZanataMessages;
 %0A%0A/**%0A *
@@ -1493,24 +1493,25 @@
 ion bean.%0A *
+ 
 %0A * @author 
@@ -1921,29 +1921,30 @@
 ate 
-Map%3CString, String%3E m
+ZanataMessages zanataM
 essa
@@ -3339,68 +3339,11 @@
     
-StatusMessage statusMessage =%0A                    new Status
+add
 Mess
@@ -3363,32 +3363,24 @@
             
-      
-  
 StatusMessag
@@ -3412,37 +3412,38 @@
             
-        null,
+zanataMessages
 %0A           
@@ -3463,54 +3463,19 @@
     
-null,%0A                            m
+.getM
 essage
-s.get
 (%22js
@@ -3522,105 +3522,8 @@
 sh%22)
-,%0A                            null);%0A            flashScope.setAttribute(%22message%22, statusMessage
 );%0A 
@@ -3628,105 +3628,19 @@
     
-StatusMessage statusMessage =%0A                    new StatusMessage(%0A                            
+addMessage(
 Stat
@@ -3688,55 +3688,22 @@
     
-        null,%0A                            null,
+zanataMessages
 %0A   
@@ -3723,36 +3723,35 @@
             
-m
+.getM
 essage
-s.get
 (%22jsf.iterat
@@ -3781,105 +3781,8 @@
 ts%22)
-,%0A                            null);%0A            flashScope.setAttribute(%22message%22, statusMessage
 );%0A 
@@ -3989,41 +3989,46 @@
     
-flashScope.setAttribute(%22message%22
+addMessage(StatusMessage.Severity.INFO
 ,%0A  
@@ -4041,17 +4041,23 @@
         
-m
+zanataM
 essages.
@@ -4059,16 +4059,23 @@
 ages.get
+Message
 (%22jsf.it
@@ -4103,32 +4103,545 @@
 rted%22));%0A    %7D%0A%0A
+    /**%0A     * Use FlashScopeBean to store message in page. Multiple ajax requests for%0A     * re-rendering statistics after updating will clear FacesMessages.%0A     * %0A     * @param severity%0A     * @param message%0A     */%0A    private void addMessage(StatusMessage.Severity severity, String message) %7B%0A        StatusMessage statusMessage =%0A                new StatusMessage(severity, null, null, message, null);%0A        statusMessage.interpolate();%0A%0A        flashScope.setAttribute(%22message%22, statusMessage);%0A    %7D%0A%0A
     public void 

Fix for copyTrans message: https://bugzilla.redhat.com/show_bug.cgi?id=1067266