Hi again,
here is my solution to manage transaction with web script, i think it should be good, but It seems strange rivet logic doesn't implemented it in their API.
So i thought there's a problem i don't see.
Any comment ?
Other Solution ?
package com.accenture;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import org.alfresco.jcr.repository.RepositoryImpl;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
public class TransactionScript extends AbstractWebScript{
public enum Action{
COMMIT,
COMMIT_TRANSACTION,
ROLLBACK_TRANSACTION,
BEGIN_TRANSACTION
}
private String user="";
private String password="";
private ServiceRegistry serviceRegistry = null;
private UserTransaction userTransaction = null;
private RepositoryImpl repository = null;
private Session session = null;
private Action action = null;
public void execute(WebScriptRequest req, WebScriptResponse res)
throws IOException {
setUser(req.getParameter("user"));
setPassword(req.getParameter("password"));
setAction(Action.valueOf(req.getParameter("action")));
Writer pw = res.getWriter();
Action currentAction = getAction();
try{
switch(currentAction){
case COMMIT:{
commit();
pw.write(currentAction.name() + " effettuato.");
break;
}
case COMMIT_TRANSACTION:{
pw.write(currentAction.name() + " effettuato.");
commitTransaction();
break;
}
case ROLLBACK_TRANSACTION:{
pw.write(currentAction.name() + " effettuato.");
rollbackTransaction();
break;
}
case BEGIN_TRANSACTION:{
pw.write(currentAction.name() + " effettuato.");
beginTransaction();
break;
}
}
} catch(LoginException e){
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (RepositoryException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (SecurityException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (IllegalStateException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (RollbackException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (HeuristicMixedException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (HeuristicRollbackException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (SystemException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
} catch (NotSupportedException e) {
pw = res.getWriter();
pw.write("Autenticazione su Alfresco non riuscita. " + currentAction.name());
e.printStackTrace(new PrintWriter(pw));
}
}
private void connect() throws LoginException, RepositoryException{
session = repository.login(new SimpleCredentials(getUser(), getPassword().toCharArray()));
}
public ServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
public void setServiceRegistry(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
public void setRepository(RepositoryImpl repository) {
this.repository = repository;
}
public RepositoryImpl getRepository() {
return repository;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public void beginTransaction() throws NotSupportedException, SystemException{
userTransaction = serviceRegistry.getTransactionService().getUserTransaction();
userTransaction.begin();
}
public void commit() throws LoginException, RepositoryException{
connect();
session.save();
close();
}
public void commitTransaction() throws LoginException, RepositoryException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException{
connect();
userTransaction.commit();
session.save();
close();
}
private void close() throws LoginException, RepositoryException{
session.logout();
serviceRegistry.getAuthenticationService().invalidateTicket(serviceRegistry.getAuthenticationService().getCurrentTicket());
serviceRegistry.getAuthenticationService().clearCurrentSecurityContext();
repository.deregisterSession();
}
public void rollbackTransaction() throws IllegalStateException, SecurityException, SystemException{
userTransaction.rollback();
}
}