@SuppressWarnings("unchecked")
  private String checkForUniqueRTT(EventRTTEventVoCollection rttColl) {
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Serializable> values = new ArrayList<Serializable>();

    DomainFactory factory = getDomainFactory();
    StringBuffer ids = new StringBuffer();
    StringBuffer exclude = new StringBuffer();
    String errors = new String();
    exclude.append(rttColl.get(0).getEvent().getID_Event());

    for (int i = 0; i < rttColl.size(); i++) {
      if (rttColl.get(i).getStatus().equals(PreActiveActiveInactiveStatus.ACTIVE)) {
        ids.append(rttColl.get(i).getRTTEvent().getID_RTTEvent());
        if (i < rttColl.size() - 1
            && rttColl.get(rttColl.size() - 1) != null
            && rttColl.get(rttColl.size() - 1).getStatusIsNotNull()
            && !rttColl
                .get(rttColl.size() - 1)
                .getStatus()
                .equals(PreActiveActiveInactiveStatus.INACTIVE)) {
          ids.append(" , ");
        }
      }
    }

    if (ids.length() == 0) return null;

    names.add("id_status");
    values.add(PreActiveActiveInactiveStatus.ACTIVE.getID());

    List rtt =
        factory.find(
            "select exRTT.event.name, exRTT.event.status, provider.systemName, exRTT.id, rtt.name from EventRTTEvent as exRTT left join exRTT.event as event left join exRTT.rTTEvent as rtt left join rtt.providerSystem as provider left join event.status as status where rtt.id in ("
                + ids
                + ") and event.id <> "
                + exclude
                + " and status.id = :id_status ",
            names,
            values);

    Iterator rttEvent = rtt.iterator();
    while (rttEvent.hasNext()) {
      Object[] obj = (Object[]) rttEvent.next();
      errors +=
          ("RTT status event  "
              + ((String) obj[2]).toUpperCase()
              + " , "
              + ((String) obj[4]).toUpperCase()
              + " is already linked to event "
              + ((String) obj[0]).toUpperCase()
              + "\n");
    }

    return errors;
  }
  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);
        }
      }
    }
  }
  private void saveRTT(
      EventVo event,
      EventRTTEventVoCollection rttToBeAdded,
      EventRTTEventVoCollection rttToBeDeteled,
      DomainFactory factory,
      Event domainRecord)
      throws StaleObjectException, ForeignKeyViolationException {
    // Save RTT
    if (rttToBeAdded != null) {
      for (int j = 0; j < rttToBeAdded.size(); j++) {
        EventRTTEvent domainRTT =
            EventRTTEventVoAssembler.extractEventRTTEvent(factory, rttToBeAdded.get(j));
        RTTEvent rttEvent = (RTTEvent) domainRTT.getRTTEvent();
        ;
        rttEvent.setUpperName(rttEvent.getName());
        domainRTT.setEvent(domainRecord);

        if (domainRTT.getStatus().equals(getDomLookup(PreActiveActiveInactiveStatus.ACTIVE))
            && domainRTT.getActivatedDateTime() == null) {
          domainRTT.setStatus(getDomLookup(PreActiveActiveInactiveStatus.ACTIVE));
          domainRTT.setActivatedBy(getUser());
          domainRTT.setActivatedDateTime(new Date());
        } else if (domainRTT
                .getStatus()
                .equals(getDomLookup(PreActiveActiveInactiveStatus.INACTIVE))
            && domainRTT.getInactivatedDateTime() == null) {
          if (domainRTT.getInactivatedBy() == null) {
            domainRTT.setInactivatedBy(getUser());
            domainRTT.setInactivatedDateTime(new Date());
          }
        }

        factory.save(domainRTT);
      }
    }
    if (rttToBeDeteled != null) {
      for (int j = 0; j < rttToBeDeteled.size(); j++) {
        EventRTTEvent domainRTT =
            EventRTTEventVoAssembler.extractEventRTTEvent(factory, rttToBeDeteled.get(j));
        factory.delete(domainRTT);
      }
    }
  }