/** @see de.ingrid.portal.portlets.admin.ContentPortlet#doViewNew(javax.portlet.RenderRequest) */
  protected boolean doViewNew(RenderRequest request) {
    try {
      AdminCMSForm f =
          (AdminCMSForm) Utils.getActionForm(request, AdminCMSForm.SESSION_KEY, AdminCMSForm.class);

      Object[] newEntity = {dbEntityClass.newInstance()};

      Context context = getContext(request);
      context.put(CONTEXT_MODE, CONTEXTV_MODE_NEW);
      context.put(CONTEXT_UTILS_STRING, new UtilsString());
      context.put(CONTEXT_ENTITIES, newEntity);

      String cmd = request.getParameter("cmd");
      if (cmd == null) {
        f.clear();
        f.clearMessages();
      }

      context.put("actionForm", f);

      setDefaultViewPage(viewNew);
      return true;
    } catch (Exception ex) {
      if (log.isErrorEnabled()) {
        log.error("Problems adding new entity:", ex);
      }
    }
    return false;
  }
  /** @see de.ingrid.portal.portlets.admin.ContentPortlet#doViewEdit(javax.portlet.RenderRequest) */
  protected boolean doViewEdit(RenderRequest request) {
    AdminCMSForm f =
        (AdminCMSForm) Utils.getActionForm(request, AdminCMSForm.SESSION_KEY, AdminCMSForm.class);
    f.clear();
    f.clearMessages();

    return super.doViewEdit(request);
  }
  /**
   * Redefine method, we have to check stuff.
   *
   * @see de.ingrid.portal.portlets.admin.ContentPortlet#doActionSave(javax.portlet.ActionRequest)
   */
  protected void doActionSave(ActionRequest request) {
    AdminCMSForm af =
        (AdminCMSForm) Utils.getActionForm(request, AdminCMSForm.SESSION_KEY, AdminCMSForm.class);
    af.populate(request);
    af.validate();
    if (af.hasErrors()) {
      return;
    }

    IngridCMS cmsEntry = new IngridCMS();
    cmsEntry.setItemChanged(new Date());
    cmsEntry.setItemChangedBy(request.getUserPrincipal().getName());
    cmsEntry.setItemDescription(af.getInput(AdminCMSForm.FIELD_DESCRIPTION));
    cmsEntry.setItemKey(af.getInput(AdminCMSForm.FIELD_KEY));

    HashSet localizedItems = new HashSet();

    String[] languages = Utils.getLanguagesShortAsArray();
    for (String lang : languages) {
      IngridCMSItem item = new IngridCMSItem();
      item.setItemLang(lang);
      item.setItemTitle(af.getInput(AdminCMSForm.FIELD_TITLE + lang));
      item.setItemValue(af.getInput(AdminCMSForm.FIELD_VALUE + lang));
      item.setItemChanged(new Date());
      item.setItemChangedBy(request.getUserPrincipal().getName());
      localizedItems.add(item);
    }

    cmsEntry.setLocalizedItems(localizedItems);

    UtilsDB.saveDBObject(cmsEntry);

    af.addMessage("admin.cms.save.success");
  }
  /**
   * @see de.ingrid.portal.portlets.admin.ContentPortlet#processAction(javax.portlet.ActionRequest,
   *     javax.portlet.ActionResponse)
   */
  public void processAction(ActionRequest request, ActionResponse response)
      throws PortletException, IOException {

    if (request.getParameter(PARAMV_ACTION_DB_DO_SAVE) != null) {
      AdminCMSForm f =
          (AdminCMSForm) Utils.getActionForm(request, AdminCMSForm.SESSION_KEY, AdminCMSForm.class);
      f.clear();
      f.clearMessages();
      // call sub method
      doActionSave(request);
      response.setRenderParameter(Settings.PARAM_ACTION, PARAMV_ACTION_DB_DO_SAVE);
      response.setRenderParameter("cmd", "action processed");
    } else if (request.getParameter(PARAMV_ACTION_DB_DO_UPDATE) != null) {
      AdminCMSForm f =
          (AdminCMSForm) Utils.getActionForm(request, AdminCMSForm.SESSION_KEY, AdminCMSForm.class);
      f.clear();
      f.clearMessages();
      // call sub method
      doActionUpdate(request);
      response.setRenderParameter(Settings.PARAM_ACTION, PARAMV_ACTION_DB_DO_UPDATE);
      response.setRenderParameter("cmd", "action processed");
    } else {
      super.processAction(request, response);
    }
  }
  /**
   * Set up Entity from parameters in request.
   *
   * @param request
   * @return
   */
  protected Object[] getDBEntities(PortletRequest request) {
    IngridCMS[] dbEntities = null;
    Long id = null;
    AdminCMSForm af =
        (AdminCMSForm) Utils.getActionForm(request, AdminCMSForm.SESSION_KEY, AdminCMSForm.class);
    try {
      id = new Long(af.getInput(AdminCMSForm.PARAM_ID));
    } catch (NumberFormatException e) {
    }
    // set up entity
    if (id != null) {
      dbEntities = (IngridCMS[]) Array.newInstance(IngridCMS.class, 1);
      dbEntities[0] = new IngridCMS();
      dbEntities[0].setId(id);
      dbEntities[0].setItemChanged(new Date());
      dbEntities[0].setItemChangedBy(request.getUserPrincipal().getName());
      dbEntities[0].setItemDescription(af.getInput(AdminCMSForm.FIELD_DESCRIPTION));
      dbEntities[0].setItemKey(af.getInput(AdminCMSForm.FIELD_KEY));

      HashSet localizedItems = new HashSet();

      String[] languages = Utils.getLanguagesShortAsArray();
      for (String lang : languages) {
        IngridCMSItem item = new IngridCMSItem();
        item.setItemLang(lang);
        item.setItemTitle(af.getInput(AdminCMSForm.FIELD_TITLE + lang));
        item.setItemValue(af.getInput(AdminCMSForm.FIELD_VALUE + lang));
        item.setItemChanged(new Date());
        item.setItemChangedBy(request.getUserPrincipal().getName());
        localizedItems.add(item);
      }
      dbEntities[0].setLocalizedItems(localizedItems);
    }

    return dbEntities;
  }
  /**
   * Redefine method, we have to check stuff.
   *
   * @see de.ingrid.portal.portlets.admin.ContentPortlet#doActionUpdate(javax.portlet.ActionRequest)
   */
  protected void doActionUpdate(ActionRequest request) {
    AdminCMSForm af =
        (AdminCMSForm) Utils.getActionForm(request, AdminCMSForm.SESSION_KEY, AdminCMSForm.class);
    af.populate(request);
    af.validate();
    if (af.hasErrors()) {
      return;
    }

    Session session = HibernateUtil.currentSession();
    IngridCMS cmsEntry =
        (IngridCMS) session.load(dbEntityClass, new Long(af.getInput(AdminCMSForm.PARAM_ID)));

    cmsEntry.setItemChanged(new Date());
    cmsEntry.setItemChangedBy(request.getUserPrincipal().getName());
    cmsEntry.setItemDescription(af.getInput(AdminCMSForm.FIELD_DESCRIPTION));
    cmsEntry.setItemKey(af.getInput(AdminCMSForm.FIELD_KEY));

    String[] languages = Utils.getLanguagesShortAsArray();
    for (String lang : languages) {
      IngridCMSItem item = cmsEntry.getLocalizedEntry(lang);
      if (item == null) {
        item = new IngridCMSItem();
        item.setItemLang(lang);
        cmsEntry.getLocalizedItems().add(item);
      }
      item.setItemTitle(af.getInput(AdminCMSForm.FIELD_TITLE + lang));
      item.setItemValue(af.getInput(AdminCMSForm.FIELD_VALUE + lang));
      item.setItemChanged(new Date());
      item.setItemChangedBy(request.getUserPrincipal().getName());
    }

    UtilsDB.updateDBObject(cmsEntry);

    af.addMessage("admin.cms.update.success");
  }