@SuppressWarnings("unchecked")
  private void saveTargetEvents(
      EventVo event, DomainFactory factory, EventTargetVoCollection eventTargets)
      throws ForeignKeyViolationException, StaleObjectException, UniqueKeyViolationException,
          UnqViolationUncheckedException {
    if (event.getID_Event() != null) {
      List oldEventsTargets =
          factory.find(
              "from EventTarget et where et.event.id = '" + event.getID_Event().toString() + "'");
      Iterator oldEventsTargetsList = oldEventsTargets.iterator();
      while (oldEventsTargetsList.hasNext()) {
        EventTarget doOldEventTarget = (EventTarget) oldEventsTargetsList.next();
        factory.delete(doOldEventTarget);
      }
    } else {
      return;
    }

    for (int i = 0; i < eventTargets.size(); i++) {
      eventTargets.get(i).getEvent().setID_Event(event.getID_Event());
      eventTargets.get(i).setID_EventTarget(null);
    }

    // Save eventTarget
    List newEventsTargets = EventTargetVoAssembler.extractEventTargetList(factory, eventTargets);
    Iterator newEventsTargetsList = newEventsTargets.iterator();

    while (newEventsTargetsList.hasNext()) {
      EventTarget doNewEventTarget = (EventTarget) newEventsTargetsList.next();
      factory.save(doNewEventTarget);
    }
  }
  private void checkForUniqRecords(
      EventVo event,
      EventExternalEventMappingVoCollection externalToBeAdded,
      EventRTTEventVoCollection rttToBeAdded)
      throws UniqueKeyViolationException {
    // Check for UNIQ only for ACTIVE records
    if (event.getStatusIsNotNull()
        && event.getStatus().equals(PreActiveActiveInactiveStatus.ACTIVE)) {
      // Check Event
      if (checkForUniqEvent(event).size() > 0)
        throw new UniqueKeyViolationException("Event Name must be unique!");

      // Check External Mappings
      if (externalToBeAdded != null && externalToBeAdded.size() > 0) {
        String err = checkForUniqueExternalMappings(externalToBeAdded);
        if (err != null && err.trim().length() > 0) {
          throw new UniqueKeyViolationException(err);
        }
      }

      // Check RTT Event
      if (rttToBeAdded != null && rttToBeAdded.size() > 0) {
        String err = checkForUniqueRTT(rttToBeAdded);
        if (err != null && err.trim().length() > 0) {
          throw new UniqueKeyViolationException(err);
        }
      }
    }
  }
  public EventVo save(
      EventVo event,
      EventTargetVoCollection eventTargets,
      EventExternalEventMappingVoCollection externalToBeAdded,
      EventExternalEventMappingVoCollection externalToBeDeleted,
      EventRTTEventVoCollection rttToBeAdded,
      EventRTTEventVoCollection rttToBeDeteled)
      throws StaleObjectException, ForeignKeyViolationException, UniqueKeyViolationException,
          DomainInterfaceException {
    if (event == null) {
      throw new DomainRuntimeException("Invalid record");
    }
    if (!event.isValidated()) {
      throw new CodingRuntimeException("Record not validated");
    }

    // CheckForUniq
    checkForUniqRecords(event, externalToBeAdded, rttToBeAdded);

    DomainFactory factory = getDomainFactory();

    if (eventTargets != null) {
      event.setEventTargets(eventTargets.toIRecordStatusTreeArray());
    }

    RecordStatusValidator helper = new RecordStatusValidator(factory, getLookupService());
    helper.validate(event);

    // Save Event
    Event domainRecord = EventVoAssembler.extractEvent(factory, event);

    if (domainRecord.getName() != null) {
      domainRecord.setUpperName(domainRecord.getName().toUpperCase());
    }

    factory.save(domainRecord);
    event = EventVoAssembler.create(domainRecord);

    // Save External
    saveExternal(event, externalToBeAdded, externalToBeDeleted, factory, domainRecord);
    // Save RTT
    saveRTT(event, rttToBeAdded, rttToBeDeteled, factory, domainRecord);
    // Save Targets
    saveTargets(event, eventTargets, factory);

    return EventVoAssembler.create(domainRecord);
  }
  @SuppressWarnings("unchecked")
  private EventVoCollection checkForUniqEvent(EventVo record) {
    DomainFactory factory = getDomainFactory();
    StringBuffer hql = new StringBuffer();
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Serializable> values = new ArrayList<Serializable>();

    hql.append(
        "select event from Event as event left join event.status as status where event.name = :name and status.id = :id_status ");
    names.add("name");
    names.add("id_status");
    values.add(record.getName());
    values.add(PreActiveActiveInactiveStatus.ACTIVE.getID());

    if (record.getID_EventIsNotNull()) {
      hql.append(" and event.id <> :id  ");
      names.add("id");
      values.add(record.getID_Event());
    }

    List collection = factory.find(hql.toString(), names, values);
    return EventVoAssembler.createEventVoCollectionFromEvent(collection);
  }