/**
   * Prints the HTML value of the field. The displayed value must be updatable by the end user. The
   * value format may be adapted to a local language. The fieldName must be used to name the html
   * form input. Never throws an Exception but log a silvertrace and writes an empty string when :
   *
   * <UL>
   *   <LI>the field type is not a managed type.
   * </UL>
   *
   * @param out
   * @param field
   * @param template
   * @param pageContext
   * @throws FormException
   */
  @Override
  public void display(
      PrintWriter out, TextField field, FieldTemplate template, PagesContext pageContext)
      throws FormException {
    String value = "";
    String html = "";

    String fieldName = template.getFieldName();
    SilverTrace.info(
        "form", "TimeFieldDisplayer.display", "root.MSG_GEN_PARAM_VALUE", "fieldName=" + fieldName);
    Map<String, String> parameters = template.getParameters(pageContext.getLanguage());
    if (field == null) {
      return;
    }
    if (!field.getTypeName().equals(TextField.TYPE)) {
      SilverTrace.info(
          "form", "TimeFieldDisplayer.display", "form.INFO_NOT_CORRECT_TYPE", TextField.TYPE);
    }

    String defaultParam = (parameters.containsKey("default") ? parameters.get("default") : "");
    String defaultValue = "";
    if ("now".equalsIgnoreCase(defaultParam) && !pageContext.isIgnoreDefaultValues()) {
      defaultValue = DateUtil.formatTime(new Date());
    }
    value = (!field.isNull() ? field.getValue(pageContext.getLanguage()) : defaultValue);
    if (pageContext.isBlankFieldsUse()) {
      value = "";
    }

    if (template.isReadOnly() && !template.isHidden()) {
      html = value;
    } else {
      input inputField = new input();
      inputField.setName(template.getFieldName());
      inputField.setID(template.getFieldName());
      inputField.setValue(EncodeHelper.javaStringToHtmlString(value));
      inputField.setType(template.isHidden() ? input.hidden : input.text);
      inputField.setMaxlength("5");
      inputField.setSize("10");
      if (template.isDisabled()) {
        inputField.setDisabled(true);
      } else if (template.isReadOnly()) {
        inputField.setReadOnly(true);
      }

      img image = null;
      if (template.isMandatory()
          && !template.isDisabled()
          && !template.isReadOnly()
          && !template.isHidden()
          && pageContext.useMandatory()) {
        image = new img();
        image.setSrc(Util.getIcon("mandatoryField"));
        image.setWidth(5);
        image.setHeight(5);
        image.setBorder(0);
      }

      // print field
      if (image != null) {
        ElementContainer container = new ElementContainer();
        container.addElement(inputField);
        container.addElement("&nbsp;");
        container.addElement(image);
        out.println(container.toString());
      } else {
        out.println(inputField.toString());
      }
    }
    out.println(html);
  }
  @Override
  public int doStartTag() throws JspException {
    if (pdcBm == null) {
      pdcBm = (PdcBm) new PdcBmImpl();
    }

    List<SearchAxis> primaryAxis = null;
    List daughters = null;

    try {
      if (StringUtil.isDefined(m_spaceOrSubSpace)) {
        List<String> availableComponentIds = new ArrayList<String>();
        SearchContext searchContext = new SearchContext();

        if (StringUtil.isDefined(componentId)) {
          primaryAxis = pdcBm.getPertinentAxisByInstanceId(searchContext, "P", componentId);
        } else {
          String a[] =
              oganisationController.getAvailCompoIds(
                  m_spaceOrSubSpace, mainSessionController.getUserId());
          availableComponentIds = Arrays.asList(a);
          if (!availableComponentIds.isEmpty()) {
            primaryAxis =
                pdcBm.getPertinentAxisByInstanceIds(searchContext, "P", availableComponentIds);
          }
        }

        if (primaryAxis != null && !primaryAxis.isEmpty()) {
          if (!"".equals(selectedAxisId)) {
            if (StringUtil.isDefined(componentId)) {
              daughters =
                  pdcBm.getPertinentDaughterValuesByInstanceId(
                      searchContext, selectedAxisId, selectedAxisPath, componentId);
            } else {
              daughters =
                  pdcBm.getPertinentDaughterValuesByInstanceIds(
                      searchContext, selectedAxisId, selectedAxisPath, availableComponentIds);
            }
          } else {
            SearchAxis searchAxis = primaryAxis.get(0);
            String axisId = new Integer(searchAxis.getAxisId()).toString();
            daughters =
                pdcBm.getPertinentDaughterValuesByInstanceIds(
                    searchContext, axisId, "", availableComponentIds);
          }
        }
      } // end if
    } catch (PdcException e) {
      throw new JspTagException("PdcException occured : " + e.toString());
    }

    try {
      pageContext.getOut().println("<tr class='intfdcolor51'>");
      pageContext.getOut().println("	<td width='100%'>");
      pageContext
          .getOut()
          .println("		<table border='0' cellspacing='0' cellpadding='0' width='100%'>");
      pageContext.getOut().println("			<tr>");
      pageContext.getOut().println("				<td>&nbsp;</td>");
      pageContext.getOut().println("				<td width='100%'><span class='txtnote'>");
      pageContext
          .getOut()
          .println("					<table cellpadding=0 cellspacing=0 border=0 width='100%'>");
      pageContext.getOut().println("						<tr><td>");

      // il peut y avoir aucun axe primaire dans un 1er temps
      if ((primaryAxis != null) && !primaryAxis.isEmpty()) {
        String text_link = "";
        for (SearchAxis searchAxis : primaryAxis) {
          String axisId = new Integer(searchAxis.getAxisId()).toString();
          String axisRootId = new Integer(searchAxis.getAxisRootId()).toString();
          String axisName = EncodeHelper.javaStringToHtmlString(searchAxis.getAxisName());

          int nbPositions = searchAxis.getNbObjects();
          if (nbPositions == 0 && componentId != null) {
            continue;
          }
          String objectLinked = "";
          String link = "javascript:top.scriptFrame.axisClick('" + axisId + "','');";
          text_link =
              webContext
                  + "/RpdcSearch/jsp/showaxishfromhomepage?query=&AxisId="
                  + axisId
                  + "&ValueId=/"
                  + axisRootId
                  + "/&SearchContext=isNotEmpty&component_id="
                  + componentId
                  + "&space_id="
                  + m_spaceOrSubSpace;
          if (axisId.equals(selectedAxisId)) {
            if (daughters != null) {
              if ("".equals(selectedAxisPath)) {
                if ("off".equals(state)) {
                  pageContext
                      .getOut()
                      .println(
                          HomePageFunctions.urlFactory(
                              link,
                              text_link,
                              axisId,
                              "",
                              objectLinked,
                              axisName,
                              "&nbsp;(" + nbPositions + ")",
                              HomePageFunctions.AXIS,
                              HomePageFunctions.AXIS_COLLAPSED,
                              "",
                              webContext));
                  continue;
                }
              }
              pageContext
                  .getOut()
                  .println(
                      HomePageFunctions.urlFactory(
                          link,
                          text_link,
                          axisId,
                          "",
                          objectLinked,
                          axisName,
                          "&nbsp;(" + nbPositions + ")",
                          HomePageFunctions.AXIS,
                          HomePageFunctions.AXIS_EXPANDED,
                          "",
                          webContext));
              // **************Begin daughters

              int d_size = daughters.size();

              int[][] tree_gui_represintation = new int[d_size][d_size];
              int selected_x = -1;
              int selected_y = 0;

              for (int j = 0; j < d_size; j++) {
                Value value = (Value) daughters.get(j);
                int valueLevel = value.getLevelNumber();
                String path = value.getFullPath();
                HomePageFunctions.setTreeNode(tree_gui_represintation, valueLevel, j, d_size);
                if (selectedAxisPath.equals(path)) {
                  selected_x = valueLevel;
                  selected_y = j;
                }
              }

              HomePageFunctions.closeTreeNodes(tree_gui_represintation, 0, d_size, d_size);
              if (selected_x >= 0) {
                if (state.equals("on")) {
                  selected_x++;
                  selected_y++;
                }
                HomePageFunctions.collapseTree(
                    tree_gui_represintation, selected_x, selected_y, d_size);
              }

              for (int j = 1; j < d_size; j++) {
                Value value = (Value) daughters.get(j);
                String valueName = EncodeHelper.javaStringToHtmlString(value.getName());
                int valueLevel = value.getLevelNumber();
                int valueNbObjects = value.getNbObjects();
                if (valueNbObjects == 0 && componentId != null) {
                  continue;
                }
                String valueFullPath = value.getFullPath();
                link =
                    "javascript:top.scriptFrame.axisClick('"
                        + axisId
                        + "','"
                        + valueFullPath
                        + "');";
                text_link =
                    webContext
                        + "/RpdcSearch/jsp/showaxishfromhomepage?query=&AxisId="
                        + axisId
                        + "&ValueId="
                        + valueFullPath
                        + "&SearchContext=isNotEmpty&component_id="
                        + componentId
                        + "&space_id="
                        + m_spaceOrSubSpace;
                if (tree_gui_represintation[0][j] == HomePageFunctions.T_HIDED_NODE) {
                  continue;
                }
                String offset =
                    HomePageFunctions.getTreeNodeOffset(
                        tree_gui_represintation, j, d_size, webContext);
                int node_type;
                if (tree_gui_represintation[valueLevel][j] == HomePageFunctions.T_OPENED_NODE) {
                  node_type = HomePageFunctions.AXIS_EXPANDED;
                } else {
                  node_type = HomePageFunctions.AXIS_COLLAPSED;
                }
                if (selectedAxisPath.equals("")) {
                  if (valueLevel == 1) {
                    pageContext
                        .getOut()
                        .println(
                            HomePageFunctions.urlFactory(
                                link,
                                text_link,
                                valueFullPath,
                                "",
                                objectLinked,
                                valueName,
                                " (" + valueNbObjects + ")",
                                HomePageFunctions.AXIS_LAST,
                                HomePageFunctions.AXIS_COLLAPSED,
                                offset,
                                webContext));
                  }
                  continue;
                } else {
                  pageContext
                      .getOut()
                      .println(
                          HomePageFunctions.urlFactory(
                              link,
                              text_link,
                              valueFullPath,
                              "",
                              objectLinked,
                              valueName,
                              " (" + valueNbObjects + ")",
                              HomePageFunctions.AXIS_LAST,
                              node_type,
                              offset,
                              webContext));
                }
              }
            }
          } else {
            pageContext
                .getOut()
                .println(
                    HomePageFunctions.urlFactory(
                        link,
                        text_link,
                        axisId,
                        "",
                        objectLinked,
                        axisName,
                        "&nbsp;(" + nbPositions + ")",
                        HomePageFunctions.AXIS,
                        HomePageFunctions.AXIS_COLLAPSED,
                        "",
                        webContext));
          }
        } // fin du for
      }

      pageContext.getOut().println("						</td></tr>");
      pageContext.getOut().println("					</table>");
      pageContext.getOut().println("				</span></td>");
      pageContext.getOut().println("			</tr>");
      pageContext.getOut().println("		</table>");
      pageContext.getOut().println("	</td>");
      pageContext.getOut().println("	<td><img src='icons/1px.gif'></td>");
      pageContext.getOut().println("	<td class='intfdcolor'><img src='icons/1px.gif'></td>");
      pageContext.getOut().println("</tr>");
    } catch (IOException ioe) {
      throw new JspTagException("IO_ERROR");
    }

    return SKIP_BODY;
  }