package org.fedorahosted.flies.webtrans.gwt;

import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;

import org.fedorahosted.flies.common.LocaleId;
import org.fedorahosted.flies.gwt.auth.AuthenticationError;
import org.fedorahosted.flies.gwt.rpc.ActivateWorkspaceAction;
import org.fedorahosted.flies.gwt.rpc.ActivateWorkspaceResult;
import org.fedorahosted.flies.security.FliesIdentity;
import org.fedorahosted.flies.webtrans.TranslationWorkspace;
import org.fedorahosted.flies.webtrans.TranslationWorkspaceManager;
import org.hibernate.Session;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;

@Name("webtrans.gwt.ActivateWorkspaceHandler")
@Scope(ScopeType.STATELESS)
public class ActivateWorkspaceHandler implements ActionHandler<ActivateWorkspaceAction, ActivateWorkspaceResult> {

	@Logger Log log;
	
	@In Session session;
	
	@In TranslationWorkspaceManager translationWorkspaceManager;

	@Override
	public ActivateWorkspaceResult execute(ActivateWorkspaceAction action, ExecutionContext context)
			throws ActionException {
		
		FliesIdentity.instance().checkLoggedIn();

		TranslationWorkspace workspace = translationWorkspaceManager.getOrRegisterWorkspace(action.getProjectContainerId().getId(), action.getLocaleId());
		
		workspace.registerTranslator(AuthenticateHandler.retrieveSessionId(), AuthenticateHandler.retrievePersonId());
		
//		HProjectContainer hProjectContainer = (HProjectContainer) session.get(HProjectContainer.class, action.getProjectContainerId().getId());
		
		return new ActivateWorkspaceResult("My Project v 1.0", "My Language");
	}

	@Override
	public Class<ActivateWorkspaceAction> getActionType() {
		return ActivateWorkspaceAction.class;
	}

	@Override
	public void rollback(ActivateWorkspaceAction action, ActivateWorkspaceResult result,
			ExecutionContext context) throws ActionException {
	}
	
}
@@ -449,16 +449,82 @@
 Result;%0A
+import org.fedorahosted.flies.repository.model.HProjectContainer;%0A
 import o
@@ -1750,10 +1750,8 @@
 %0A%09%09%0A
-//
 %09%09HP
@@ -1893,76 +1893,604 @@
 %09%0A%09%09
-return new ActivateWorkspaceResult(%22My Project v 1.0%22, %22My Language%22
+String iterationName = (String)session.createQuery(%0A%09%09%09%09%22select it.name %22 +%0A%09%09%09%09%22from HProjectIteration it %22 +%0A%09%09%09%09%22where it.container.id = :containerId %22%0A%09%09%09%09)%0A%09%09%09%09.setParameter(%22containerId%22, hProjectContainer.getId())%0A%09%09%09%09.uniqueResult();%0A%09%09%0A%09%09String projectName = (String)session.createQuery(%0A%09%09%09%09%22select it.project.name %22 +%0A%09%09%09%09%22from HProjectIteration it %22 +%0A%09%09%09%09%22where it.container.id = :containerId %22%0A%09%09%09%09)%0A%09%09%09%09.setParameter(%22containerId%22, hProjectContainer.getId())%0A%09%09%09%09.uniqueResult();%0A%0A%09%09return new ActivateWorkspaceResult(projectName+%22 %22+iterationName, workspace.getLocale().toString()
 );%0A%09

Make the ActivateWorkspaceHandler return the project name+iteration name and locale id