public static RecurrenceRule makeRule(
      Delegator delegator, int frequency, int interval, int count, long endTime)
      throws RecurrenceRuleException {
    String freq[] = {"", "SECONDLY", "MINUTELY", "HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY"};

    if (frequency < 1 || frequency > 7) throw new RecurrenceRuleException("Invalid frequency");
    if (interval < 0) throw new RecurrenceRuleException("Invalid interval");

    String freqStr = freq[frequency];

    try {
      GenericValue value = delegator.makeValue("RecurrenceRule");

      value.set("frequency", freqStr);
      value.set("intervalNumber", Long.valueOf(interval));
      value.set("countNumber", Long.valueOf(count));
      if (endTime > 0) {
        value.set("untilDateTime", new java.sql.Timestamp(endTime));
      }
      delegator.createSetNextSeqId(value);
      RecurrenceRule newRule = new RecurrenceRule(value);
      return newRule;
    } catch (GenericEntityException ee) {
      throw new RecurrenceRuleException(ee.getMessage(), ee);
    } catch (RecurrenceRuleException re) {
      throw re;
    }
  }