/**
  * This method invokes the correct edit dialog according to the supplied action type.
  *
  * @param action The action to edit
  */
 private void showActionEditDialog(KeywordAction action) {
   BaseActionDialog dialog;
   switch (action.getType()) {
     case FORWARD:
       dialog = new ForwardActionDialog(ui, this);
       break;
     case JOIN:
       dialog = new JoinGroupActionDialog(ui, this);
       break;
     case LEAVE:
       dialog = new JoinGroupActionDialog(ui, this);
       break;
     case REPLY:
       dialog = new ReplyActionDialog(ui, this);
       break;
     case EXTERNAL_CMD:
       dialog = new ExternalCommandActionDialog(ui, this);
       break;
     case EMAIL:
       dialog = new EmailActionDialog(ui, this);
       break;
     default:
       throw new IllegalStateException();
   }
   dialog.init(action);
   dialog.show();
 }
 /** @see net.frontlinesms.data.repository.KeywordActionDao#incrementCounter(KeywordAction) */
 public void incrementCounter(KeywordAction action) {
   String incrementCounterQuery =
       "UPDATE "
           + KeywordAction.TABLE_NAME
           + " as action"
           + " SET "
           + KeywordAction.Field.COUNTER
           + "="
           + KeywordAction.Field.COUNTER
           + "+1"
           + " WHERE action=?";
   super.getHibernateTemplate().bulkUpdate(incrementCounterQuery, action);
   action.incrementCounter();
 }
  public void showSelectedKeyword() {
    Object selected = ui.getSelectedItem(keywordListComponent);

    Object divider = find(COMPONENT_KEYWORDS_DIVIDER);
    if (ui.getItems(divider).length >= 2) {
      ui.remove(ui.getItems(divider)[ui.getItems(divider).length - 1]);
    }

    // If selected is null, then we are here because a keyword has been unselected
    if (selected == null) {
      enableKeywordFields(ui.find(COMPONENT_KEY_PANEL));
      return;
    }

    // An existent keyword is selected, let's check if it is simple or advanced.
    Keyword keyword = ui.getAttachedObject(selected, Keyword.class);
    Collection<KeywordAction> actions = this.keywordActionDao.getActions(keyword);
    boolean simple = actions.size() <= 3;
    if (simple) {
      KeywordAction.Type previousType = null;
      for (KeywordAction action : actions) {
        KeywordAction.Type type = action.getType();
        if (type != KeywordAction.Type.REPLY
            && type != KeywordAction.Type.JOIN
            && type != KeywordAction.Type.LEAVE) {
          simple = false;
          break;
        }

        if (action.getEndDate() != DEFAULT_END_DATE) {
          simple = false;
          break;
        }

        if (type == previousType) {
          simple = false;
          break;
        }

        previousType = type;
      }
    }

    String keywordDescription = getDisplayableDescription(keyword);
    if (simple) {
      Object panel = ui.loadComponentFromFile(UI_FILE_KEYWORDS_SIMPLE_VIEW, this);
      ui.add(divider, panel);

      // Fill every field
      Object tfKeyword = ui.find(panel, COMPONENT_TF_KEYWORD);
      Object lbKeywordDescription = ui.find(panel, COMPONENT_LB_KEYWORD_DESCRIPTION);
      ui.setEnabled(tfKeyword, false);
      ui.setText(tfKeyword, getDisplayableKeyword(keyword));

      // We display the keyword description in the panel
      if (keywordDescription != null && keywordDescription.length() > 0) {
        ui.setText(lbKeywordDescription, keywordDescription);
      } else {
        ui.remove(lbKeywordDescription);
      }

      // We have to set the text in case there is no join/leave keyword actions
      setJoinGroupDisplay(null);
      setLeaveGroupDisplay(null);

      for (KeywordAction action : actions) {
        KeywordAction.Type type = action.getType();
        if (type == KeywordAction.Type.REPLY) {
          Object cbReply = ui.find(panel, COMPONENT_CB_AUTO_REPLY);
          Object tfReply = ui.find(panel, COMPONENT_TF_AUTO_REPLY);
          ui.setSelected(cbReply, true);
          ui.setText(tfReply, action.getUnformattedReplyText());
        } else if (type == KeywordAction.Type.JOIN) {
          setJoinGroupDisplay(action.getGroup());
        } else if (type == KeywordAction.Type.LEAVE) {
          setLeaveGroupDisplay(action.getGroup());
        }
      }
    } else {
      Object panel = ui.loadComponentFromFile(UI_FILE_KEYWORDS_ADVANCED_VIEW, this);
      Object table = ui.find(panel, COMPONENT_ACTION_LIST);
      Object lbKeywordDescription = ui.find(panel, COMPONENT_LB_KEYWORD_DESCRIPTION);

      // We display the keyword description in the panel
      if (keywordDescription != null && keywordDescription.length() > 0) {
        ui.setText(lbKeywordDescription, keywordDescription);
      } else {
        ui.remove(lbKeywordDescription);
      }
      ui.setText(
          panel,
          InternationalisationUtils.getI18nString(
              COMMON_KEYWORD_ACTIONS_OF, getDisplayableKeyword(keyword)));
      // Fill every field
      for (KeywordAction action : actions) {
        ui.add(table, ui.getRow(action));
      }
      ui.add(divider, panel);
      enableKeywordActionFields(table, ui.find(panel, COMPONENT_KEY_ACT_PANEL));
    }
    enableKeywordFields(ui.find(COMPONENT_KEY_PANEL));
  }
  public void keywordTab_doSave(Object panel) {
    log.trace("ENTER");
    long startDate;
    try {
      startDate =
          InternationalisationUtils.parseDate(InternationalisationUtils.getDefaultStartDate())
              .getTime();
    } catch (ParseException e) {
      log.debug("We never should get this", e);
      log.trace("EXIT");
      return;
    }

    // Get the KeywordAction details
    String replyText = keywordSimple_getAutoReply(panel);
    Group joinGroup = keywordSimple_getJoin();
    Group leaveGroup = keywordSimple_getLeave();

    // Get the keyword attached to the selected item.  If the "Add Keyword" option is selected,
    // there will be no keyword attached to it.
    Keyword keyword = null;
    Object selectedKeywordItem = ui.getSelectedItem(keywordListComponent);
    if (selectedKeywordItem != null) keyword = ui.getKeyword(selectedKeywordItem);

    if (keyword == null) {
      // Adding keyword as well as actions
      String newkeyword = ui.getText(ui.find(panel, COMPONENT_TF_KEYWORD));
      try {
        keyword = new Keyword(newkeyword, "");
        this.keywordDao.saveKeyword(keyword);
      } catch (DuplicateKeyException e) {
        ui.alert(InternationalisationUtils.getI18nString(MESSAGE_KEYWORD_EXISTS));
        log.trace("EXIT");
        return;
      }
      keywordTab_doClear(panel);
    } else {
      // Editing an existent keyword.  This keyword may already have actions applied to it, so
      // we need to check for actions and update them as appropriate.
      KeywordAction replyAction =
          this.keywordActionDao.getAction(keyword, KeywordAction.Type.REPLY);
      if (replyAction != null) {
        if (replyText == null) {
          // The reply action has been removed
          keywordActionDao.deleteKeywordAction(replyAction);
        } else {
          replyAction.setReplyText(replyText);
          this.keywordActionDao.updateKeywordAction(replyAction);
          // We set null to don't add it in the end
          replyText = null;
        }
      }

      KeywordAction joinAction = this.keywordActionDao.getAction(keyword, KeywordAction.Type.JOIN);
      if (joinAction != null) {
        if (joinGroup == null) {
          // Previous join action has been removed, so delete it.
          keywordActionDao.deleteKeywordAction(joinAction);
        } else {
          // Group to join has been updated
          joinAction.setGroup(joinGroup);
          this.keywordActionDao.updateKeywordAction(joinAction);
          // Join Group has been handled, so unset it.
          joinGroup = null;
        }
      }

      KeywordAction leaveAction =
          this.keywordActionDao.getAction(keyword, KeywordAction.Type.LEAVE);
      if (leaveAction != null) {
        if (leaveGroup == null) {
          keywordActionDao.deleteKeywordAction(leaveAction);
        } else {
          leaveAction.setGroup(leaveGroup);
          this.keywordActionDao.updateKeywordAction(leaveAction);
          // We set null to don't add it in the end
          leaveGroup = null;
        }
      }
    }

    // Handle creation of new KeywordActions if required
    if (replyText != null) {
      KeywordAction action =
          KeywordAction.createReplyAction(keyword, replyText, startDate, DEFAULT_END_DATE);
      keywordActionDao.saveKeywordAction(action);
    }
    if (joinGroup != null) {
      KeywordAction action =
          KeywordAction.createGroupJoinAction(keyword, joinGroup, startDate, DEFAULT_END_DATE);
      keywordActionDao.saveKeywordAction(action);
    }
    if (leaveGroup != null) {
      KeywordAction action =
          KeywordAction.createGroupLeaveAction(keyword, leaveGroup, startDate, DEFAULT_END_DATE);
      keywordActionDao.saveKeywordAction(action);
    }

    // Refresh the UI
    updateKeywordList();
    ui.infoMessage(InternationalisationUtils.getI18nString(MESSAGE_KEYWORD_SAVED));
    log.trace("EXIT");
  }