View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.taglib;
20  
21  import org.apache.struts.taglib.TagUtils;
22  import org.itracker.model.util.IssueUtilities;
23  import org.itracker.model.util.ProjectUtilities;
24  import org.itracker.web.util.Constants;
25  
26  import javax.servlet.http.HttpSession;
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.tagext.BodyTagSupport;
29  import java.util.Locale;
30  import java.util.MissingResourceException;
31  
32  
33  /**
34   * Formats a resolution for a display.  If the project uses fixed resolutions,
35   * it prints the appropriate string for the current locale.  Currently the tag
36   * only supports non-editable resolution fields.
37   */
38  public class FormatResolutionTag extends BodyTagSupport {
39      /**
40       *
41       */
42      private static final long serialVersionUID = 1L;
43      public static final String DISPLAY_TYPE_EDIT = "edit";
44      public static final String DISPLAY_TYPE_VIEW = "view";
45  
46      private String text = null;
47      private String displayType;
48      private int projectOptions;
49  
50      public int getProjectOptions() {
51          return projectOptions;
52      }
53  
54      public void setProjectOptions(int value) {
55          projectOptions = value;
56      }
57  
58      public String getDisplayType() {
59          return displayType;
60      }
61  
62      public void setDisplayType(String value) {
63          displayType = value;
64      }
65  
66      public int doStartTag() throws JspException {
67          text = null;
68          return EVAL_BODY_BUFFERED;
69      }
70  
71      public int doAfterBody() throws JspException {
72          if (bodyContent != null) {
73              String value = bodyContent.getString().trim();
74              if (value.length() > 0) {
75                  text = value;
76              }
77          }
78          return SKIP_BODY;
79      }
80  
81      public int doEndTag() throws JspException {
82          StringBuffer results = new StringBuffer();
83          if (text != null && !text.trim().equals("")) {
84              Locale locale = null;
85  
86              HttpSession session = pageContext.getSession();
87              if (session != null) {
88                  locale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
89              }
90  
91              try {
92  
93                  projectOptions = ProjectUtilities.OPTION_PREDEFINED_RESOLUTIONS;
94              } catch (NumberFormatException nfe) {
95              }
96  
97              if (ProjectUtilities.hasOption(ProjectUtilities.OPTION_PREDEFINED_RESOLUTIONS, projectOptions)) {
98                  try {
99                      text = IssueUtilities.checkResolutionName(text, locale);
100                 } catch (MissingResourceException mre) {
101                     // Key didn't exist so just stick the key in as a real number.
102                 }
103             }
104             results.append(text);
105         }
106         // ResponseUtils.write(pageContext, results.toString());
107         TagUtils.getInstance().write(pageContext, results.toString());
108         clearState();
109         return (EVAL_PAGE);
110     }
111 
112     public void release() {
113         super.release();
114         clearState();
115     }
116 
117     private void clearState() {
118         text = null;
119         displayType = DISPLAY_TYPE_VIEW;
120         projectOptions = 0;
121     }
122 }