PageRenderTime 110ms CodeModel.GetById 13ms app.highlight 85ms RepoModel.GetById 1ms app.codeStats 1ms

/factory/factory-core/src/main/java/org/qualipso/factory/eventqueue/EventQueueServiceBean.java

https://github.com/moustictoto/Ulysse
Java | 1111 lines | 681 code | 153 blank | 277 comment | 139 complexity | 5749a0993eebf0aa655d51d91cb70f44 MD5 | raw file
   1/*
   2 * Qualipso Funky Factory
   3 * Copyright (C) 2006-2010 INRIA
   4 * http://www.inria.fr - molli@loria.fr
   5 *
   6 * This is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU Lesser General Public License version 3
   8 * as published by the Free Software Foundation. See the GNU
   9 * Lesser General Public License in LGPL.txt for more details.
  10 *
  11 * You should have received a copy of the GNU Lesser General Public
  12 * License along with this software; if not, write to the Free
  13 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  14 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  15 *
  16 * Initial authors :
  17 *
  18 *Elamri Firas
  19 *Yuksel Huriye
  20 *Secondary authors :
  21 *Amrou Mohanned Khalifa
  22 *Yiqing Li
  23 */
  24package org.qualipso.factory.eventqueue;
  25
  26import java.util.ArrayList;
  27import java.util.Date;
  28import java.util.Iterator;
  29import java.util.UUID;
  30
  31import javax.annotation.Resource;
  32import javax.ejb.EJB;
  33import javax.ejb.SessionContext;
  34import javax.ejb.Stateless;
  35import javax.jws.WebMethod;
  36import javax.jws.WebService;
  37import javax.jws.soap.SOAPBinding;
  38import javax.jws.soap.SOAPBinding.Style;
  39import javax.persistence.EntityManager;
  40import javax.persistence.PersistenceContext;
  41
  42import org.apache.commons.logging.Log;
  43import org.apache.commons.logging.LogFactory;
  44import org.jboss.ejb3.annotation.SecurityDomain;
  45import org.jboss.ws.annotation.EndpointConfig;
  46import org.jboss.wsf.spi.annotation.WebContext;
  47import org.qualipso.factory.FactoryException;
  48import org.qualipso.factory.FactoryNamingConvention;
  49import org.qualipso.factory.FactoryResource;
  50import org.qualipso.factory.FactoryResourceIdentifier;
  51import org.qualipso.factory.FactoryResourceProperty;
  52import org.qualipso.factory.binding.BindingService;
  53import org.qualipso.factory.binding.PathHelper;
  54import org.qualipso.factory.core.CoreServiceException;
  55import org.qualipso.factory.eventqueue.entity.Event;
  56import org.qualipso.factory.eventqueue.entity.EventQueue;
  57import org.qualipso.factory.membership.MembershipService;
  58import org.qualipso.factory.notification.NotificationService;
  59import org.qualipso.factory.security.pap.PAPService;
  60import org.qualipso.factory.security.pap.PAPServiceHelper;
  61import org.qualipso.factory.security.pep.PEPService;
  62
  63/**
  64 * Implementation of the ClockService. Provides a time service for the factory.
  65 * 
  66 * @author <a href="mailto:christophe.bouthier@loria.fr">Christophe Bouthier</a>
  67 * @author Nicolas HENRY
  68 * @author Marlène HANTZ
  69 * @date 27 July 2009
  70 */
  71@Stateless(name = EventQueueService.SERVICE_NAME, mappedName = FactoryNamingConvention.SERVICE_PREFIX + EventQueueService.SERVICE_NAME)
  72@WebService(endpointInterface = "org.qualipso.factory.eventqueue.EventQueueService", targetNamespace = FactoryNamingConvention.SERVICE_NAMESPACE
  73        + EventQueueService.SERVICE_NAME, serviceName = EventQueueService.SERVICE_NAME)
  74@WebContext(contextRoot = FactoryNamingConvention.WEB_SERVICE_CORE_MODULE_CONTEXT, urlPattern = FactoryNamingConvention.WEB_SERVICE_URL_PATTERN_PREFIX
  75        + EventQueueService.SERVICE_NAME)
  76@SOAPBinding(style = Style.RPC)
  77@SecurityDomain(value = "JBossWSDigest")
  78@EndpointConfig(configName = "Standard WSSecurity Endpoint")
  79public class EventQueueServiceBean implements EventQueueService {
  80
  81    private static Log logger = LogFactory.getLog(EventQueueServiceBean.class);
  82
  83    private BindingService binding;
  84    private PEPService pep;
  85    private PAPService pap;
  86    private NotificationService notification;
  87    private MembershipService membership;
  88    private SessionContext ctx;
  89    private EntityManager em;
  90
  91    public EventQueueServiceBean() {
  92    }
  93
  94    /**
  95     * cette methode positionne l Entity Manager
  96     * 
  97     * @param em
  98     *            Entity Manager
  99     */
 100    @PersistenceContext
 101    public void setEntityManager(EntityManager em) {
 102        this.em = em;
 103    }
 104
 105    /**
 106     * cette methode retourne l Entity Manager
 107     * 
 108     * @return em l'entity manager
 109     */
 110    public EntityManager getEntityManager() {
 111        return this.em;
 112    }
 113
 114    /**
 115     * cette methode positionne la Session Context
 116     * 
 117     * @param ctx
 118     *            la Session Context
 119     */
 120    @Resource
 121    public void setSessionContext(SessionContext ctx) {
 122        this.ctx = ctx;
 123    }
 124
 125    /**
 126     * cette methode retourne la Session Context
 127     * 
 128     * @return la Session Context
 129     */
 130    public SessionContext getSessionContext() {
 131        return this.ctx;
 132    }
 133
 134    /**
 135     * cette methode positionne le Binding Service
 136     * 
 137     * @param binding
 138     *            le Binding Service
 139     * 
 140     */
 141    @EJB
 142    public void setBindingService(BindingService binding) {
 143        this.binding = binding;
 144    }
 145
 146    /**
 147     * 
 148     *cette methode retourne le Binding Service
 149     * 
 150     * @return le Binding Service
 151     */
 152    public BindingService getBindingService() {
 153        return this.binding;
 154    }
 155
 156    /**
 157     * cette methode positionne le PEP Service
 158     * 
 159     * @param pep
 160     *            le PEP Service
 161     * 
 162     */
 163    @EJB
 164    public void setPEPService(PEPService pep) {
 165        this.pep = pep;
 166    }
 167
 168    /**
 169     * cette methode retourne le PEP Service
 170     * 
 171     * @return PEP Service
 172     */
 173    public PEPService getPEPService() {
 174        return this.pep;
 175    }
 176
 177    /**
 178     * cette methode positionne le PAP Service
 179     * 
 180     * @param pap
 181     *            le PAP Service
 182     * 
 183     */
 184    @EJB
 185    public void setPAPService(PAPService pap) {
 186        this.pap = pap;
 187    }
 188
 189    /**
 190     * cette methode retourne le PAP Service
 191     * 
 192     * @return PAPS ervice
 193     */
 194    public PAPService getPAPService() {
 195        return this.pap;
 196    }
 197
 198    /**
 199     * cette methode positionne le Notification Service
 200     * 
 201     * @param notification
 202     *            le Notification Service
 203     * 
 204     */
 205    @EJB
 206    public void setNotificationService(NotificationService notification) {
 207        this.notification = notification;
 208    }
 209
 210    /**
 211     * cette methode retourne le Notification Service
 212     * 
 213     * @return le Service Notification
 214     */
 215    public NotificationService getNotificationService() {
 216        return this.notification;
 217    }
 218
 219    /**
 220     * cette methode positionne le Membership Service
 221     * 
 222     * @param membership
 223     *            le Membership Service
 224     * 
 225     */
 226    @EJB
 227    public void setMembershipService(MembershipService membership) {
 228        this.membership = membership;
 229    }
 230
 231    /**
 232     * cette methode retourne le Membership Service
 233     * 
 234     * @return le Membership Service
 235     */
 236    public MembershipService getMembershipService() {
 237        return this.membership;
 238    }
 239
 240    /**
 241     * cette methode retourne les evenements contenu dans la queue sinon une
 242     * exception si la queue n est pas trouve
 243     * 
 244     * @param name
 245     *            le nom de la queue
 246     * @return returne un tableau des evenements contenu dans la queue
 247     */
 248    @Override
 249    public Event[] getEvents(String path) throws EventQueueServiceException {
 250
 251        FactoryResourceIdentifier identifier;
 252        try {
 253            identifier = binding.lookup(path);
 254            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 255                String caller = membership.getProfilePathForConnectedIdentifier();
 256                pep.checkSecurity(caller, path, "read");
 257
 258                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 259                if (eventqueue == null) {
 260                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 261                }
 262
 263                Event[] evs = new Event[eventqueue.getEvents().size()];
 264                
 265                /*ArrayList<Event> evts = new ArrayList<Event>();
 266                ArrayList<Event> allEvts = eventqueue.getEvents();
 267                for (Event event : allEvts) {
 268                	try{
 269                		pep.checkSecurity(caller,event.getFromResource(), "read");
 270                		evts.add(event);
 271                	}catch(PEPServiceException e){
 272                		
 273                	}
 274				}*/
 275                
 276                return eventqueue.getEvents().toArray(evs);
 277
 278            } else {
 279                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 280            }
 281        } catch (Exception e) {
 282            logger.error("unable to get events", e);
 283            ctx.setRollbackOnly();
 284            throw new EventQueueServiceException("unable to get events", e);
 285        }
 286    }
 287
 288    /**
 289     * cette méthode permet de creer une nouvelle queue a partir de son nom
 290     * sinon retourne une exception si la queue n a pas pu etre cree la queue
 291     * est rendu persistante et bindee par la suite
 292     * 
 293     * @param name
 294     *            le nom de la nouvelle queue a creer
 295     * 
 296     */
 297    @Override
 298    public void createEventQueue(String path) throws EventQueueServiceException {
 299        logger.info("createEventQueue(...) called");
 300        logger.debug("params : path=" + path);
 301        try {
 302
 303            String caller = membership.getProfilePathForConnectedIdentifier();
 304            pep.checkSecurity(caller, PathHelper.getParentPath(path), "create");
 305            EventQueue evq = new EventQueue();
 306            evq.setId(UUID.randomUUID().toString());
 307            evq.setEvents(new ArrayList<Event>());
 308            em.persist(evq);
 309
 310            binding.bind(evq.getFactoryResourceIdentifier(), path);
 311
 312            String policyId = UUID.randomUUID().toString();
 313            pap.createPolicy(policyId, PAPServiceHelper.addRuleToPolicy(PAPServiceHelper.buildOwnerPolicy(policyId, caller, path), "", new String[] { "create",
 314                    "read", "update" }));
 315            binding.setProperty(path, FactoryResourceProperty.OWNER, caller);
 316            binding.setProperty(path, FactoryResourceProperty.POLICY_ID, policyId);
 317
 318        } catch (Exception e) {
 319            logger.error("unable to create an event queue", e);
 320            ctx.setRollbackOnly();
 321            throw new EventQueueServiceException("unable to create an event queue", e);
 322        }
 323    }
 324
 325    /**
 326     * cette methode place un evenement dans la queue associe au name sinon
 327     * retourne une exception si l evenement n est pas ajoute
 328     * 
 329     * @param name
 330     *            le nom de l'event queue
 331     * @param event
 332     *            l evenement a pusher dans la queue
 333     * 
 334     */
 335    @Override
 336    public void pushEvent(String path, Event event) throws EventQueueServiceException {
 337
 338        FactoryResourceIdentifier identifier;
 339        try {
 340            if (event == null) {
 341                throw new IllegalArgumentException("event can't be null");
 342            }
 343            identifier = binding.lookup(path);
 344            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 345                String caller = membership.getProfilePathForConnectedIdentifier();
 346                pep.checkSecurity(caller, path, "update");
 347
 348                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 349                if (eventqueue == null) {
 350                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 351                }
 352
 353                eventqueue.getEvents().add(event);
 354
 355                em.persist(eventqueue);
 356            } else {
 357                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 358            }
 359        } catch (Exception e) {
 360            logger.error("unable to pushEvent an event in the event queue", e);
 361            ctx.setRollbackOnly();
 362            throw new EventQueueServiceException("unable to pushEvent an event in the event queue", e);
 363        }
 364    }
 365
 366    /**
 367     * cette methode retourne l event queue associe a path sinon une exception
 368     * si l'event queue n'est pas trouve
 369     * 
 370     * @param path
 371     *            le path de l event queue
 372     * @return event queue associé a path
 373     */
 374    @Override
 375    public FactoryResource findResource(String path) throws FactoryException {
 376        logger.info("findResource(...) called");
 377        logger.debug("params : path=" + path);
 378
 379        FactoryResourceIdentifier identifier = binding.lookup(path);
 380
 381        if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 382            String caller = membership.getProfilePathForConnectedIdentifier();
 383            pep.checkSecurity(caller, path, "read");
 384
 385            EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 386            if (eventqueue == null) {
 387                throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 388            }
 389
 390            return eventqueue;
 391        }
 392
 393        throw new EventQueueServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 394    }
 395
 396    @Override
 397    public Event getLastEvent(String path) throws EventQueueServiceException {
 398
 399        FactoryResourceIdentifier identifier;
 400        try {
 401            identifier = binding.lookup(path);
 402            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 403                String caller = membership.getProfilePathForConnectedIdentifier();
 404                pep.checkSecurity(caller, path, "read");
 405
 406                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 407                if (eventqueue == null) {
 408                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 409                }
 410
 411                int size = eventqueue.getEvents().size();
 412                Event[] evq = new Event[size];
 413                evq = eventqueue.getEvents().toArray(evq);
 414                Event result = evq[size - 1];
 415                return result;
 416
 417            } else {
 418                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 419            }
 420        } catch (Exception e) {
 421            logger.error("unable to get Last Event", e);
 422            ctx.setRollbackOnly();
 423            throw new EventQueueServiceException("unable to get Last Event", e);
 424        }
 425
 426    }
 427
 428    /**
 429     * cette methode retourne le Resource Type List
 430     * 
 431     * @return Resource Type List
 432     */
 433    @Override
 434    public String[] getResourceTypeList() {
 435        return RESOURCE_TYPE_LIST;
 436    }
 437
 438    /**
 439     * cette methode retourne le nom du service
 440     * 
 441     * @return le nom du cervice
 442     */
 443    @Override
 444    public String getServiceName() {
 445        return SERVICE_NAME;
 446    }
 447
 448    @Override
 449    public void deleteEvent(String path, Event e) throws EventQueueServiceException {
 450
 451        FactoryResourceIdentifier identifier;
 452
 453        try {
 454            identifier = binding.lookup(path);
 455            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 456                String caller = membership.getProfilePathForConnectedIdentifier();
 457                pep.checkSecurity(caller, path, "update");
 458
 459                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 460                if (eventqueue == null) {
 461                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 462                }
 463                /*
 464                 * int size = eventqueue.getEvents().size(); Event[] evq = new
 465                 * Event[size]; evq = eventqueue.getEvents().toArray(evq);
 466                 */
 467                ArrayList<Event> newEventList = eventqueue.getEvents();
 468                if (newEventList.remove(e)) {
 469                    eventqueue.setEvents(newEventList);
 470                    em.merge(eventqueue);
 471                } else {
 472                    throw new EventQueueServiceException("this event doesn't exist in this queue");
 473                }
 474
 475            } else {
 476                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 477            }
 478        } catch (Exception ex) {
 479            logger.error("unable to delete Event", ex);
 480            ctx.setRollbackOnly();
 481            throw new EventQueueServiceException("unable to delete Event", ex);
 482        }
 483
 484    }
 485
 486    /**
 487     * 
 488     * return an array of event that have or contains the same ressourceType
 489     * 
 490     * @param path
 491     *            path of the EventQueue
 492     * @param ressourceType
 493     *            the type of resource event
 494     * @param substring
 495     *            true if typeRssource contains parameter typeRessource, false
 496     *            if typeRssource contains exactly the parameter typeRessource
 497     * @return array of event
 498     * @throws EventQueueServiceException
 499     */
 500    @WebMethod
 501    public Event[] findEventByRessourceType(String path, String ressourceType, boolean substring) throws EventQueueServiceException {
 502
 503        FactoryResourceIdentifier identifier;
 504        try {
 505            // li des cas exceptionnelles
 506            if ((path == null) || (ressourceType == null))
 507                throw new EventQueueServiceException("Le parametre n'est pas correct verifier le path ou le ressourceType");
 508
 509            identifier = binding.lookup(path);
 510            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 511                String caller = membership.getProfilePathForConnectedIdentifier();
 512                pep.checkSecurity(caller, path, "read");
 513
 514                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 515                if (eventqueue == null) {
 516                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 517                }
 518
 519                ArrayList<Event> resultContains = new ArrayList<Event>();
 520                ArrayList<Event> resultEquals = new ArrayList<Event>();
 521
 522                ArrayList<Event> listEvent = eventqueue.getEvents();
 523                Iterator<Event> it = listEvent.iterator();
 524
 525                while (it.hasNext()) {
 526                    Event ev = it.next();
 527
 528                    if (substring == true && ev.getResourceType().contains(ressourceType)) {
 529                        resultContains.add(ev);
 530
 531                    }
 532
 533                    if (substring == false && ev.getResourceType().equals(ressourceType)) {
 534                        resultEquals.add(ev);
 535                    }
 536                }
 537
 538                if (substring == true) {
 539                    return (Event[]) (resultContains.toArray());
 540                } else {
 541                    return (Event[]) (resultEquals.toArray());
 542                }
 543            }
 544
 545            else {
 546                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 547            }
 548        } catch (Exception e) {
 549            logger.error("unable to  find Event By Ressource Type", e);
 550            ctx.setRollbackOnly();
 551            throw new EventQueueServiceException("unable to  find Event By Ressource Type", e);
 552        }
 553
 554    }
 555
 556    /**
 557     * return an array of event that have or contains the same thrower
 558     * 
 559     * @param path
 560     *            path of the EventQueue
 561     * @param thrower
 562     *            thrower of event
 563     * @param substring
 564     *            true if thrower contains the string parameter thrower , false
 565     *            if thrower contains exactly the parameter thrower.
 566     * @return array of event (return an array of event that have or contains
 567     *         the same thrower)
 568     * @throws EventQueueServiceException
 569     */
 570    @WebMethod
 571    public Event[] findEventBythrower(String path, String thrower, boolean substring) throws EventQueueServiceException {
 572
 573        FactoryResourceIdentifier identifier;
 574        try {
 575            identifier = binding.lookup(path);
 576            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 577                String caller = membership.getProfilePathForConnectedIdentifier();
 578                pep.checkSecurity(caller, path, "read");
 579
 580                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 581                if (eventqueue == null) {
 582                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 583                }
 584
 585                ArrayList<Event> resultContains = new ArrayList<Event>();
 586                ArrayList<Event> resultFalse = new ArrayList<Event>();
 587
 588                ArrayList<Event> listEvent = eventqueue.getEvents();
 589                Iterator<Event> it = listEvent.iterator();
 590
 591                while (it.hasNext()) {
 592                    Event ev = it.next();
 593
 594                    if (substring == true && ev.getThrowedBy().contains(thrower)) {
 595                        resultContains.add(ev);
 596                    }
 597
 598                    if (substring == false && ev.getThrowedBy().equals(thrower)) {
 599                        resultFalse.add(ev);
 600                    }
 601                }
 602
 603                if (substring == true) {
 604                    Event[] tab = new Event[resultContains.size()];
 605                    int i = 0;
 606                    while (i < resultContains.size()) {
 607                        tab[i] = resultContains.get(i);
 608                        i++;
 609                    }
 610                    return tab;
 611                } else {
 612                    Event[] tab = new Event[resultFalse.size()];
 613                    int i = 0;
 614                    while (i < resultFalse.size()) {
 615                        tab[i] = resultFalse.get(i);
 616                        i++;
 617                    }
 618                    return tab;
 619                }
 620            }
 621
 622            else {
 623                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 624            }
 625        } catch (Exception e) {
 626            logger.error("unable to find Event By thrower", e);
 627            ctx.setRollbackOnly();
 628            throw new EventQueueServiceException("unable to find Event By thrower", e);
 629        }
 630    }
 631
 632    /**
 633     * 
 634     * @param path
 635     *            path of the EventQueue
 636     * @param fromRessource
 637     *            fromRessource of event
 638     * @param substring
 639     *            true if fromRessource contains the string parameter
 640     *            fromRessource , false if thrower contains exactly the
 641     *            parameter fromRessource.
 642     * @return array of event ( return an array of event that have or contains
 643     *         the same fromRessource )
 644     * @throws EventQueueServiceException
 645     */
 646    @WebMethod
 647    public Event[] findEventByFromRessource(String path, String fromRessource, boolean substring) throws EventQueueServiceException {
 648        FactoryResourceIdentifier identifier;
 649        try {
 650            // li des cas exceptionnelles
 651            if ((path == null) || (fromRessource == null))
 652                throw new EventQueueServiceException("Le parametre n'est pas correct verifier le path ou fromRessource");
 653
 654            identifier = binding.lookup(path);
 655            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 656                String caller = membership.getProfilePathForConnectedIdentifier();
 657                pep.checkSecurity(caller, path, "read");
 658
 659                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 660                if (eventqueue == null) {
 661                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 662                }
 663
 664                ArrayList<Event> resultContains = new ArrayList<Event>();
 665                ArrayList<Event> resultFalse = new ArrayList<Event>();
 666
 667                ArrayList<Event> listEvent = eventqueue.getEvents();
 668                Iterator<Event> it = listEvent.iterator();
 669
 670                while (it.hasNext()) {
 671                    Event ev = it.next();
 672
 673                    if (substring == true && ev.getFromResource().contains(fromRessource)) {
 674                        resultContains.add(ev);
 675                    }
 676
 677                    if (substring == false && ev.getFromResource().equals(fromRessource)) {
 678                        resultFalse.add(ev);
 679                    }
 680                }
 681
 682                if (substring == true) {
 683                    Event[] tab = new Event[resultContains.size()];
 684                    int i = 0;
 685                    while (i < resultContains.size()) {
 686                        tab[i] = resultContains.get(i);
 687                        i++;
 688                    }
 689                    return tab;
 690                } else {
 691                    Event[] tab = new Event[resultFalse.size()];
 692                    int i = 0;
 693                    while (i < resultFalse.size()) {
 694                        tab[i] = resultFalse.get(i);
 695                        i++;
 696                    }
 697                    return tab;
 698                }
 699            }
 700
 701            else {
 702                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 703            }
 704        } catch (Exception e) {
 705            logger.error("unable to find Event By from Ressource", e);
 706            ctx.setRollbackOnly();
 707            throw new EventQueueServiceException("unable to find Event By from Ressource", e);
 708        }
 709    }
 710
 711    /**
 712     * 
 713     * @param path
 714     *            path of the EventQueue
 715     * @param date
 716     *            date of the event
 717     * @return array of event ( return an array of event having the same date )
 718     * @throws EventQueueServiceException
 719     */
 720    @WebMethod
 721    public Event[] findEventByDate(String path, Date date) throws EventQueueServiceException {
 722        FactoryResourceIdentifier identifier;
 723        try {
 724            identifier = binding.lookup(path);
 725            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 726                String caller = membership.getProfilePathForConnectedIdentifier();
 727                pep.checkSecurity(caller, path, "read");
 728
 729                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 730                if (eventqueue == null) {
 731                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 732                }
 733
 734                ArrayList<Event> result = new ArrayList<Event>();
 735
 736                ArrayList<Event> listEvent = eventqueue.getEvents();
 737                Iterator<Event> it = listEvent.iterator();
 738
 739                while (it.hasNext()) {
 740                    Event ev = it.next();
 741                    if (ev.getDate().equals(date)) {
 742                        result.add(ev);
 743                    }
 744
 745                }
 746
 747                Event[] tab = new Event[result.size()];
 748                int i = 0;
 749                while (i < result.size()) {
 750                    tab[i] = result.get(i);
 751                    i++;
 752                }
 753                // return (Event[]) (result.toArray());
 754                return tab;
 755            }
 756
 757            else {
 758                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 759            }
 760        } catch (Exception e) {
 761            logger.error("unable to find Event By Date", e);
 762            ctx.setRollbackOnly();
 763            throw new EventQueueServiceException("unable to find Event By Date", e);
 764        }
 765
 766    }
 767
 768    /**
 769     * 
 770     * @param path
 771     *            path of the EventQueue
 772     * @param date
 773     *            date of the event
 774     * @return array of event (return an array of event having date lesser or
 775     *         equal of the parameter date )
 776     * @throws EventQueueServiceException
 777     */
 778
 779    @WebMethod
 780    public Event[] findEventByDateInf(String path, Date date) throws EventQueueServiceException {
 781        FactoryResourceIdentifier identifier;
 782        try {
 783            identifier = binding.lookup(path);
 784            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 785                String caller = membership.getProfilePathForConnectedIdentifier();
 786                pep.checkSecurity(caller, path, "read");
 787
 788                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 789                if (eventqueue == null) {
 790                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 791                }
 792
 793                ArrayList<Event> result = new ArrayList<Event>();
 794
 795                ArrayList<Event> listEvent = eventqueue.getEvents();
 796                Iterator<Event> it = listEvent.iterator();
 797
 798                while (it.hasNext()) {
 799                    Event ev = (Event) it.next();
 800                    if (ev.getDate().before(date) || ev.getDate().equals(date)) {
 801                        result.add(ev);
 802                    }
 803
 804                }// while
 805
 806                // return (Event[]) (result.toArray());
 807
 808                Event[] tab = new Event[result.size()];
 809                int i = 0;
 810                while (i < result.size()) {
 811                    tab[i] = result.get(i);
 812                    i++;
 813                }
 814                return tab;
 815            }
 816
 817            else {
 818                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 819            }
 820        } catch (Exception e) {
 821            logger.error("unable to find Event By DateSup", e);
 822            ctx.setRollbackOnly();
 823            throw new EventQueueServiceException("unable to find Event By DateSup", e);
 824        }
 825    }
 826
 827    /**
 828     * 
 829     * @param path
 830     *            path of the EventQueue
 831     * @param date
 832     *            date date of the event
 833     * @return array of event (return an array of event having date superior or
 834     *         equal of the parameter date )
 835     * @throws EventQueueServiceException
 836     */
 837    @WebMethod
 838    public Event[] findEventByDateSup(String path, Date date) throws EventQueueServiceException {
 839        FactoryResourceIdentifier identifier;
 840        try {
 841            identifier = binding.lookup(path);
 842            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 843                String caller = membership.getProfilePathForConnectedIdentifier();
 844                pep.checkSecurity(caller, path, "read");
 845
 846                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 847                if (eventqueue == null) {
 848                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 849                }
 850
 851                ArrayList<Event> result = new ArrayList<Event>();
 852
 853                ArrayList<Event> listEvent = eventqueue.getEvents();
 854                Iterator<Event> it = listEvent.iterator();
 855
 856                while (it.hasNext()) {
 857                    Event ev = it.next();
 858                    if (ev.getDate().after(date) || ev.getDate().equals(date)) {
 859                        result.add(ev);
 860                    }
 861
 862                }// while
 863
 864                Event[] tab = new Event[result.size()];
 865                int i = 0;
 866                while (i < result.size()) {
 867                    tab[i] = result.get(i);
 868                    i++;
 869                }
 870                return tab;
 871            }
 872
 873            else {
 874                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 875            }
 876        } catch (Exception e) {
 877            logger.error("unable to find Event By DateInf", e);
 878            ctx.setRollbackOnly();
 879            throw new EventQueueServiceException("unable to find Event By DateInf", e);
 880        }
 881    }
 882
 883    /**
 884     * 
 885     * @param path
 886     *            path of the EventQueue
 887     * @param date1
 888     *            date of the event
 889     * @param date2
 890     *            date of the event
 891     * @return array of event (return an array of event having date between
 892     *         date1 and date2 where date1<= date2)
 893     * @throws EventQueueServiceException
 894     */
 895    @WebMethod
 896    public Event[] findEventByDateBetween(String path, Date date1, Date date2) throws EventQueueServiceException {
 897        FactoryResourceIdentifier identifier;
 898        try {
 899            identifier = binding.lookup(path);
 900            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 901                String caller = membership.getProfilePathForConnectedIdentifier();
 902                pep.checkSecurity(caller, path, "read");
 903
 904                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 905                if (eventqueue == null) {
 906                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 907                }
 908
 909                ArrayList<Event> result = new ArrayList<Event>();
 910
 911                ArrayList<Event> listEvent = eventqueue.getEvents();
 912                Iterator<Event> it = listEvent.iterator();
 913                while (it.hasNext()) {
 914                    Event ev = it.next();
 915
 916                    // correction d'un bug concernant date1 et date2 et inversé
 917                    // if (((ev.getDate().before(date1)) ||
 918                    // (ev.getDate().equals(date1))) &&
 919                    // ((ev.getDate().after(date2)) ||
 920                    // (ev.getDate().equals(date2)))) {
 921                    // result.add(ev);
 922                    // }
 923
 924                    if ((ev.getDate().getTime() >= date1.getTime()) && (ev.getDate().getTime() <= date2.getTime())) {
 925                        result.add(ev);
 926                    }
 927
 928                }// while
 929
 930                Event[] tab = new Event[result.size()];
 931                int i = 0;
 932                while (i < result.size()) {
 933                    tab[i] = result.get(i);
 934                    i++;
 935                }
 936                return tab;
 937            }
 938
 939            else {
 940                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 941            }
 942        } catch (Exception e) {
 943            logger.error("unable to find Event By Date Between", e);
 944            ctx.setRollbackOnly();
 945            throw new EventQueueServiceException("unable to find Event By Date Between", e);
 946        }
 947    }
 948
 949    /**
 950     * 
 951     * @param path
 952     * @param eventType
 953     * @param substring
 954     * @return
 955     * @throws EventQueueServiceException
 956     */
 957    @WebMethod
 958    public Event[] findEventByEventType(String path, String eventType, boolean substring) throws EventQueueServiceException {
 959        FactoryResourceIdentifier identifier;
 960        try {
 961            // li des cas exceptionnelles
 962            if ((path == null) || (eventType == null))
 963                throw new EventQueueServiceException("Le parametre n'est pas correct . Verifier le path ou l'eventType");
 964
 965            identifier = binding.lookup(path);
 966            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 967                String caller = membership.getProfilePathForConnectedIdentifier();
 968                pep.checkSecurity(caller, path, "read");
 969
 970                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 971                if (eventqueue == null) {
 972                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 973                }
 974
 975                ArrayList<Event> resultContains = new ArrayList<Event>();
 976                ArrayList<Event> resultEquals = new ArrayList<Event>();
 977
 978                ArrayList<Event> listEvent = eventqueue.getEvents();
 979                Iterator<Event> it = listEvent.iterator();
 980
 981                while (it.hasNext()) {
 982                    Event ev = it.next();
 983                    if ((substring == true) && (ev.getEventType().contains(eventType))) {
 984                        resultContains.add(ev);
 985                    }
 986
 987                    if ((substring == false) && (ev.getEventType().equals(eventType))) {
 988                        resultEquals.add(ev);
 989                    }
 990                }// while
 991
 992                if (substring == true) {
 993                    Event[] tab = new Event[resultContains.size()];
 994                    int i = 0;
 995                    while (i < resultContains.size()) {
 996                        tab[i] = resultContains.get(i);
 997                        i++;
 998                    }
 999                    return tab;
1000                } else {
1001                    Event[] tab = new Event[resultEquals.size()];
1002                    int i = 0;
1003                    while (i < resultEquals.size()) {
1004                        tab[i] = resultEquals.get(i);
1005                        i++;
1006                    }
1007                    return tab;
1008                }
1009            }
1010
1011            else {
1012                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
1013            }
1014        } catch (Exception e) {
1015            logger.error("unable to find Event By EventType", e);
1016            ctx.setRollbackOnly();
1017            throw new EventQueueServiceException("unable to find Event By EventType", e);
1018        }
1019    }
1020
1021    public Event[] findObjectEvent(String path, Event event) throws EventQueueServiceException {
1022
1023        FactoryResourceIdentifier identifier;
1024        try {
1025            identifier = binding.lookup(path);
1026            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
1027                String caller = membership.getProfilePathForConnectedIdentifier();
1028                pep.checkSecurity(caller, path, "read");
1029
1030                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
1031                if (eventqueue == null) {
1032                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
1033                }
1034
1035                ArrayList<Event> result = new ArrayList<Event>();
1036                ArrayList<Event> listEvent = eventqueue.getEvents();
1037                Iterator<Event> it = listEvent.iterator();
1038
1039                while (it.hasNext()) {
1040                    Event ev = it.next();
1041                    if (ev.equals(event)) {
1042                        result.add(ev);
1043                    }
1044                }
1045
1046                Event[] tab = new Event[result.size()];
1047                int i = 0;
1048                while (i < result.size()) {
1049                    tab[i] = result.get(i);
1050                    i++;
1051                }
1052                return tab;
1053            }
1054
1055            else {
1056                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
1057            }
1058        } catch (Exception e) {
1059            logger.error("unable to find Object Event", e);
1060            ctx.setRollbackOnly();
1061            throw new EventQueueServiceException("unable to find Object Event", e);
1062        }
1063    }
1064
1065    @Override
1066    public void removeQueue(String path) throws EventQueueServiceException {
1067
1068        FactoryResourceIdentifier identifier;
1069        try {
1070            identifier = binding.lookup(path);
1071            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
1072                String caller = membership.getProfilePathForConnectedIdentifier();
1073                pep.checkSecurity(caller, path, "update");
1074
1075                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
1076                if (eventqueue == null) {
1077                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
1078                }
1079
1080                em.remove(eventqueue);
1081                binding.unbind(path);
1082            } else {
1083                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
1084            }
1085        } catch (Exception e) {
1086            logger.error("unable to remove  event queue", e);
1087            ctx.setRollbackOnly();
1088            throw new EventQueueServiceException("unable to create an event queue", e);
1089        }
1090
1091    }
1092
1093    @WebMethod
1094    public Event[] findEventBySimpleParameter(String path, String eventType, String thrower, String resourceType, String fromRessource, Date date,
1095            boolean dateSup, boolean dateInf) throws EventQueueServiceException {
1096        return null;
1097    }
1098
1099    @WebMethod
1100    public Event[] findEventByComposedParameter(String path, String eventType, String thrower, String resourceType, String fromRessource, Date date1, Date date2)
1101            throws EventQueueServiceException {
1102        return null;
1103    }
1104
1105    @Override
1106    public void generateXML(String path) {
1107        // TODO Auto-generated method stub
1108
1109    }
1110
1111}