Subversion Repositories bacoAlunos

Rev

Rev 1969 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.examples;/*
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
 *
 *
 * Created at 17/Out/2003 , 22:56:03
 *
 */

/**
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
 *
 *
 * Created at 17/Out/2003 , 22:56:03
 *
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import pt.utl.ist.berserk.logic.filterManager.exceptions.FilterManagerException;
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.FilterChainFailedException;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.ServiceManagerException;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.views.AnnouncementView;
import pt.estgp.estgweb.services.authenticate.AuthenticateException;
import pt.estgp.estgweb.web.UserSessionProxy;
import pt.estgp.estgweb.web.utils.RequestUtils;

public class ServiceCall implements 1.5.0/docs/api/java/lang/Runnable.html">Runnable
{

    private static 1.5.0/docs/api/java/lang/String.html">String readRequester() throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader br_reader = new 1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader(new 1.5.0/docs/api/java/io/InputStreamReader.html">InputStreamReader(1.5.0/docs/api/java/lang/System.html">System.in));
        1.5.0/docs/api/java/lang/System.html">System.out.println("Please type the requester");
        return br_reader.readLine();
    }

    private static 1.5.0/docs/api/java/lang/String.html">String readUserName() throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader br_reader = new 1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader(new 1.5.0/docs/api/java/io/InputStreamReader.html">InputStreamReader(1.5.0/docs/api/java/lang/System.html">System.in));
        1.5.0/docs/api/java/lang/System.html">System.out.println("Please type the username");
        return br_reader.readLine();
    }
    private static 1.5.0/docs/api/java/lang/String.html">String readPassword() throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader br_reader = new 1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader(new 1.5.0/docs/api/java/io/InputStreamReader.html">InputStreamReader(1.5.0/docs/api/java/lang/System.html">System.in));
        1.5.0/docs/api/java/lang/System.html">System.out.println("Please type the password");
        return br_reader.readLine();
    }


    public static void main(1.5.0/docs/api/java/lang/String.html">String[] mainArgs) throws  1.5.0/docs/api/java/io/IOException.html">IOException, ServiceManagerException,FilterManagerException
    {


        try
        {
            //Getting parameters
            1.5.0/docs/api/java/lang/String.html">String requester = readRequester();
            1.5.0/docs/api/java/lang/String.html">String username = readUserName();
            1.5.0/docs/api/java/lang/String.html">String password = readPassword();

           
            //Creating a user
            User u = DomainObjectFactory.createUserImpl();
            u.setPassword(password);
            u.setName(username);
            u.setRoles("admin");
            u.setUsername(username);
            5+0%2Fdocs%2Fapi+Object">Object[] args = {u};
            IServiceManager sm = ServiceManager.getInstance();
            try
            {
                sm.execute(requester,"CreateUser",args);
            }
            catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
            {
                1.5.0/docs/api/java/lang/System.html">System.out.println("Already exist fail creation, gone login now");
            }


            //Authentication
            try
            {
                //our service will be called with the string we want to write to the stdout
                args = new 5+0%2Fdocs%2Fapi+Object">Object[]{username,password};
                1.5.0/docs/api/java/lang/String.html">String result = (1.5.0/docs/api/java/lang/String.html">String) sm.execute(requester,"Authenticate",args);
                1.5.0/docs/api/java/lang/System.html">System.out.println(result);
            }
            catch(AuthenticateException e)
            {
                if(e.getMessage().equals(AuthenticateException.ALREADY_AUTHENTICATED))
                    1.5.0/docs/api/java/lang/System.html">System.out.println("Already AuthenticatedUsers");
                else
                    throw e;
            }

            //Creating an announcement
            Announcement announcement = DomainObjectFactory.createAnnouncementImpl();
            announcement.setText("Anuncio de teste");
            announcement.setTitle("Titulo de Anuncio de teste");

            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"object"};
            args = new 5+0%2Fdocs%2Fapi+Object">Object[]{announcement};
            sm.execute(requester,"CreateAnnouncement",args,names);


            //Load an announcement view
            args = new 5+0%2Fdocs%2Fapi+Object">Object[]{(long) 1};
            names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            AnnouncementView aView = (AnnouncementView) sm.execute(requester,"LoadAnnouncementView",args,names);
            1.5.0/docs/api/java/lang/System.html">System.out.println("View of annoucement 1 loaded:" + aView.getTitle());


            //Testing creating a view to change database
            AnnouncementView a = new AnnouncementView();
            a.setId(1);
            a.setText("Anuncio de testeEditado");
            List<String> targets = new ArrayList<String>();
            targets.add("admin");
            targets.add("teacher");
            a.setTargetRoles(targets);

            args = new 5+0%2Fdocs%2Fapi+Object">Object[]{a};
            names = new 1.5.0/docs/api/java/lang/String.html">String[]{"object"};
            sm.execute(requester,"EditAnnouncement",args,names);




            args = new 5+0%2Fdocs%2Fapi+Object">Object[]{requester};
            UserSessionImpl sess = (UserSessionImpl) sm.execute(requester,"LoadUserSession",args);

            sess.put("teste2","testeXML");
            sess.serialize(requester);
        }
        catch (ServiceManagerException e)
        {
            if (e instanceof FilterChainFailedException)
                1.5.0/docs/api/java/lang/System.html">System.out.println("Access Denied.");
            else throw e;
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable throwable) {
            throwable.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

//      public static void main(String[] mainArgs) throws  IOException, ServiceManagerException,FilterManagerException
//      {
//        try
//              {
//                      String value = pt.estgp.estgweb.examples.ServiceCall.readRequester();
//            Thread t = new Thread(new pt.estgp.estgweb.examples.ServiceCall());
//            t.start();
//            IServiceManager sm;
//                      sm = ServiceManager.getInstance();
//                      //our service will be called with the string we want to write to the stdout
//                      Object[] args = {"Hello World A"};
//                      String result = (String) sm.execute(value,"ServiceStdoutWriter",args);
//            System.out.println(result);
//        }
//              catch (ServiceManagerException e)
//              {
//                      if (e instanceof FilterChainFailedException)
//                              System.out.println("Access Denied.");
//                      else throw e;
    //          }
    //
    //  }
    public static void go(1.5.0/docs/api/java/lang/String.html">String[] mainArgs) throws  1.5.0/docs/api/java/io/IOException.html">IOException, ServiceManagerException,FilterManagerException
    {
        try
        {
            1.5.0/docs/api/java/lang/String.html">String value = readRequester();

            IServiceManager sm;
            sm = ServiceManager.getInstance();
            //our service will be called with the string we want to write to the stdout
            5+0%2Fdocs%2Fapi+Object">Object[] args = {"Hello World B"};
            sm.execute(value,"ServiceStdoutWriter",args);
        }
        catch (ServiceManagerException e)
        {
            if (e instanceof FilterChainFailedException)
                1.5.0/docs/api/java/lang/System.html">System.out.println("Access Denied.");
            else throw e;
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable throwable) {
            throwable.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public void run() {
        try {
            go(null);
        }
        catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (ServiceManagerException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        catch (FilterManagerException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}