public IUINotification createUINotification(IQueuedNotification notification) throws Exception {
    if (notification == null) throw new CodingRuntimeException("Invalid notification.");

    if (notification.getINotificationMessage() == null)
      throw new CodingRuntimeException("Notification message is null.");

    if (notification.getINotificationPriority() == null)
      throw new CodingRuntimeException("Notification priority is null.");

    NotificationVo instance = new NotificationVo();
    DomainFactory domainFactory = this.getDomainFactory();

    AppUser doUser =
        (AppUser)
            domainFactory.getDomainObject(AppUser.class, notification.getINotificationUserId());

    if (doUser == null)
      throw new DomainRuntimeException("Invalid User Id passed into createNotification.");

    instance.setUser(AppUserNotificationVoAssembler.create(doUser));
    instance.setDateTime(new DateTime());
    instance.setNotificationPriority(notification.getINotificationPriority().getId());
    instance.setMessage(notification.getINotificationMessage());
    instance.setSource(notification.getINotificationSource());
    instance.setSeen(Boolean.FALSE);

    if (notification.getINotificationEntityType() != null) {
      instance.setEntityType(notification.getINotificationEntityType());
      instance.setEntityId(notification.getINotificationEntityId());
    }

    String[] errors = instance.validate();
    if (errors != null && errors.length > 0) {
      throw new RuntimeException("Validation errors while creating a user notification.");
    }

    try {
      ims.core.admin.domain.objects.Notifications doNotification =
          NotificationVoAssembler.extractNotifications(domainFactory, instance);
      domainFactory.save(doNotification);
      instance = NotificationVoAssembler.create(doNotification);
    } catch (StaleObjectException e) {
      throw new RuntimeException(e);
    }

    return instance;
  }
  public void setUserNotified(INotification notification) {
    if (notification == null)
      throw new CodingRuntimeException("Notification passed into setUserNotified is null.");

    DomainFactory factory = getDomainFactory();

    NotificationVo notificationVo =
        NotificationVoAssembler.create(
            (Notifications)
                factory.getDomainObject(Notifications.class, notification.getINotificationId()));

    notificationVo.setUserNotified(true);

    notificationVo.validate();

    try {
      factory.save(NotificationVoAssembler.extractNotifications(factory, notificationVo));
    } catch (StaleObjectException e) {
      throw new RuntimeException(e);
    }
  }
  public void markAsSeen(IUINotification notification) {
    if (notification == null)
      throw new CodingRuntimeException("Notification passed into markAsSeen is null.");

    DomainFactory factory = getDomainFactory();

    NotificationVo notificationVo =
        NotificationVoAssembler.create(
            (Notifications)
                factory.getDomainObject(Notifications.class, notification.getINotificationId()));

    notificationVo.setSeen(Boolean.TRUE);
    notificationVo.setSeenAt(new DateTime());

    notificationVo.validate();

    try {
      factory.save(NotificationVoAssembler.extractNotifications(factory, notificationVo));
    } catch (StaleObjectException e) {
      throw new RuntimeException(e);
    }
  }
  @SuppressWarnings("unchecked")
  public IUINotification[] getNotifications(
      int userId, boolean seen, int maxNumberOfNotifications) {
    DomainFactory factory = getDomainFactory();

    String hql = "from Notifications n";
    StringBuffer condStr = new StringBuffer();
    String andStr = "";

    ArrayList<String> markers = new ArrayList<String>();
    ArrayList<Serializable> values = new ArrayList<Serializable>();

    List userNotificationsList = null;

    condStr.append(andStr + " n.user.id = :userIden");
    markers.add("userIden");
    values.add(userId);
    andStr = " and";

    condStr.append(andStr + " n.seen = :isSeen");
    markers.add("isSeen");
    values.add(seen);
    andStr = " and";

    hql += " where";
    hql += condStr.toString();

    hql += " order by n.dateTime desc";

    if (maxNumberOfNotifications > EnvironmentConfig.getResultSetDefaultMax())
      maxNumberOfNotifications = EnvironmentConfig.getResultSetDefaultMax();

    userNotificationsList = factory.find(hql, markers, values, maxNumberOfNotifications);

    return NotificationVoAssembler.createNotificationVoCollectionFromNotifications(
            userNotificationsList)
        .toIUINotificationArray();
  }
  @SuppressWarnings("unchecked")
  public IUINotification[] getNotifications(
      int userId,
      Date dateFrom,
      Date dateTo,
      String message,
      String source,
      NotificationPriority priority,
      boolean seen) {
    DomainFactory factory = getDomainFactory();

    String hql = "from Notifications n";
    StringBuffer condStr = new StringBuffer();
    String andStr = "";

    ArrayList<String> markers = new ArrayList<String>();
    ArrayList<Serializable> values = new ArrayList<Serializable>();

    List userNotificationsList = null;

    condStr.append(andStr + " n.user.id = :userIden");
    markers.add("userIden");
    values.add(userId);
    andStr = " and";

    if (dateFrom != null) {
      condStr.append(andStr + " n.dateTime >= :from");
      markers.add("from");
      values.add(dateFrom.getDate());
      dateFrom.getDate().toString();
      andStr = " and";
    }

    if (dateTo != null) {
      Date dateToClone = ((Date) dateTo.clone()).addDay(1);
      condStr.append(andStr + " n.dateTime < :to");
      markers.add("to");
      values.add(dateToClone.getDate());
      andStr = " and";
    }

    if (message != null) {
      condStr.append(andStr + " n.message like :mess");
      markers.add("mess");
      values.add("%" + message + "%");
      andStr = " and";
    }

    if (source != null) {
      condStr.append(andStr + " n.source like :src");
      markers.add("src");
      values.add("%" + source + "%");
      andStr = " and";
    }

    if (priority != null) {
      condStr.append(andStr + " n.priority = :pri");
      markers.add("pri");
      values.add(priority.getId());
      andStr = " and";
    }

    condStr.append(andStr + " n.seen = :isSeen");
    markers.add("isSeen");
    values.add(seen);
    andStr = " and";

    hql += " where";
    hql += condStr.toString();

    hql += " order by n.priority asc, n.dateTime desc";

    userNotificationsList = factory.find(hql, markers, values);

    return NotificationVoAssembler.createNotificationVoCollectionFromNotifications(
            userNotificationsList)
        .toIUINotificationArray();
  }