Subversion Repositories WebE

Rev

Rev 33 | Rev 35 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 PointedEar 1
package ch.ffhs.webE.action;
2
 
3
import java.util.ArrayList;
33 PointedEar 4
import java.util.Date;
31 PointedEar 5
import java.util.List;
33 PointedEar 6
import java.util.Map;
31 PointedEar 7
 
8
import javax.servlet.http.HttpServletRequest;
9
 
10
import org.apache.struts2.StrutsStatics;
11
 
12
import ch.ffhs.webE.dao.TermDAOImpl;
33 PointedEar 13
import ch.ffhs.webE.dao.UserDAOImpl;
14
import ch.ffhs.webE.domain.ObjectEntity;
15
import ch.ffhs.webE.domain.ObjectType;
31 PointedEar 16
import ch.ffhs.webE.domain.Term;
33 PointedEar 17
import ch.ffhs.webE.domain.User;
31 PointedEar 18
 
19
import com.opensymphony.xwork2.Action;
20
import com.opensymphony.xwork2.ActionContext;
21
import com.opensymphony.xwork2.ActionSupport;
22
import com.opensymphony.xwork2.ModelDriven;
23
 
24
/**
25
 * Implements actions applicable to term editing
26
 *
27
 * @author Thomas Lahn
28
 */
29
public class TermAction extends ActionSupport implements ModelDriven<Term>
30
{
33 PointedEar 31
  private static final long serialVersionUID = 1L;
31 PointedEar 32
 
33
  private Term term = new Term();
34
  private List<Term> termList = new ArrayList<Term>();
33 PointedEar 35
  private final TermDAOImpl termDAO = new TermDAOImpl();
36
  private final UserDAOImpl userDAO = new UserDAOImpl();
31 PointedEar 37
 
33 PointedEar 38
  /**
39
   * Session object
40
   */
41
  Map<String, Object> session = ActionContext.getContext().getSession();
42
 
34 PointedEar 43
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
44
      .getContext().get(StrutsStatics.HTTP_REQUEST);
45
 
31 PointedEar 46
  /*
47
   * (non-Javadoc)
48
   *
49
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
50
   */
51
  public Term getModel()
52
  {
53
    return this.term;
54
  }
55
 
56
  /**
57
   * Executes the DB query to save the user
58
   *
59
   * @return {@link Action#SUCCESS}
60
   */
34 PointedEar 61
  public String save()
31 PointedEar 62
  {
34 PointedEar 63
    if (!"1".equals(this.request.getParameter("edit")))
64
    {
65
      User user = this.userDAO.searchUsername((String) this.session
66
          .get("username"));
67
      ObjectEntity obj = new ObjectEntity(user,
68
          new ObjectType(ObjectType.TERM), user, null, new Date(), false,
69
          this.term, null, null);
70
      this.term.setObject(obj);
71
    }
72
 
33 PointedEar 73
    if (this.termDAO.saveOrUpdate(this.term))
74
    {
75
      return Action.SUCCESS;
76
    }
77
 
78
    return Action.ERROR;
31 PointedEar 79
  }
80
 
81
  /**
33 PointedEar 82
   * DB query for term list
31 PointedEar 83
   *
84
   * @return SUCCESS
85
   */
86
  public String list()
87
  {
34 PointedEar 88
    this.termList = this.termDAO.getTerms();
31 PointedEar 89
    return Action.SUCCESS;
90
  }
91
 
92
  /**
93
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
94
   *         otherwise
95
   */
96
  public String edit()
97
  {
98
    int id = this.getIdParameter();
99
 
100
    if (id > 0)
101
    {
33 PointedEar 102
      this.term = this.termDAO.getTermById(id);
103
      if (this.term != null)
104
      {
34 PointedEar 105
        this.term.edit = true;
33 PointedEar 106
        return Action.SUCCESS;
107
      }
31 PointedEar 108
    }
33 PointedEar 109
 
110
    return Action.ERROR;
31 PointedEar 111
  }
112
 
113
  /**
114
   * Gets the ID Parameter for update / delete requests
115
   *
116
   * @return int from the ID request. If not set or wrong, it gives back -1
117
   */
118
  private int getIdParameter()
119
  {
120
    int id = -1;
121
    try
122
    {
34 PointedEar 123
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
31 PointedEar 124
    }
125
    catch (Exception e)
126
    {
127
      /* TODO: Logging - wrong parameter set */
128
    }
129
 
130
    return id;
131
  }
132
 
133
  /**
134
   * deletes a user, gets the ID from the "id" parameter that was submitted with
135
   * the HTTP request
136
   *
137
   * @return String - either SUCCESS or ERROR constant
138
   */
139
  public String delete()
140
  {
141
 
142
    int id = this.getIdParameter();
143
 
144
    /* Check for malicious ID values */
145
    if (id > 0)
146
    {
147
      this.termDAO.deleteTerm(id);
148
      return Action.SUCCESS;
149
    }
150
    else
151
    {
152
      return Action.ERROR;
153
    }
154
  }
155
 
156
  /*
157
   * Standard getters and setters
158
   */
159
 
160
  /**
161
   * @return The term edited with this instance
162
   */
163
  public Term getTerm()
164
  {
165
    return this.term;
166
  }
167
 
168
  /**
169
   * @param term
170
   *          The term edited with this instance
171
   */
172
  public void setTerm(Term term)
173
  {
174
    this.term = term;
175
  }
176
 
177
  /**
178
   * @return The list of terms edited with this instance
179
   */
180
  public List<Term> getTermList()
181
  {
182
    return this.termList;
183
  }
184
 
185
  /**
186
   * @param termList
187
   *          The list of terms edited with this instance
188
   */
189
  public void setTermList(List<Term> termList)
190
  {
191
    this.termList = termList;
192
  }
193
}