Subversion Repositories WebE

Rev

Rev 34 | 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
 
35 PointedEar 43
  /**
44
   * @var <code>true</code> if the term is edited/renamed, <code>false</code>
45
   *      otherwise
46
   */
47
  public boolean edit = false;
48
 
49
  /**
50
   * @var <code>true</code> if a term was added, <code>false</code> otherwise
51
   */
52
  public boolean added = false;
53
 
34 PointedEar 54
  private final HttpServletRequest request = (HttpServletRequest) ActionContext
55
      .getContext().get(StrutsStatics.HTTP_REQUEST);
56
 
35 PointedEar 57
  /**
58
   * The term that was just saved (added, renamed)
59
   */
60
  public Term savedTerm;
61
 
31 PointedEar 62
  /*
63
   * (non-Javadoc)
64
   *
65
   * @see com.opensymphony.xwork2.ModelDriven#getModel()
66
   */
67
  public Term getModel()
68
  {
69
    return this.term;
70
  }
71
 
72
  /**
35 PointedEar 73
   * DB query for term list
74
   *
75
   * @return SUCCESS
76
   */
77
  public String list()
78
  {
79
    this.termList = this.termDAO.getTerms();
80
    return Action.SUCCESS;
81
  }
82
 
83
  /**
31 PointedEar 84
   * Executes the DB query to save the user
85
   *
86
   * @return {@link Action#SUCCESS}
87
   */
34 PointedEar 88
  public String save()
31 PointedEar 89
  {
35 PointedEar 90
    User user = this.userDAO.searchUsername((String) this.session
91
        .get("username"));
92
 
93
    if ("false".equals(this.request.getParameter("edit")))
34 PointedEar 94
    {
35 PointedEar 95
      /* Add a new term */
34 PointedEar 96
      ObjectEntity obj = new ObjectEntity(user,
97
          new ObjectType(ObjectType.TERM), user, null, new Date(), false,
98
          this.term, null, null);
99
      this.term.setObject(obj);
35 PointedEar 100
      this.added = true;
34 PointedEar 101
    }
102
 
35 PointedEar 103
    this.edit = false;
104
 
105
    String result = Action.SUCCESS;
106
    if (!this.termDAO.saveOrUpdate(this.term))
33 PointedEar 107
    {
35 PointedEar 108
      result = Action.ERROR;
33 PointedEar 109
    }
110
 
35 PointedEar 111
    this.savedTerm = this.term;
112
    this.term = null;
31 PointedEar 113
 
35 PointedEar 114
    this.list();
115
 
116
    return result;
31 PointedEar 117
  }
118
 
119
  /**
120
   * @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
121
   *         otherwise
122
   */
123
  public String edit()
124
  {
125
    int id = this.getIdParameter();
126
 
35 PointedEar 127
    String result = Action.ERROR;
31 PointedEar 128
    if (id > 0)
129
    {
33 PointedEar 130
      this.term = this.termDAO.getTermById(id);
131
      if (this.term != null)
132
      {
35 PointedEar 133
        this.edit = true;
134
        result = Action.SUCCESS;
33 PointedEar 135
      }
31 PointedEar 136
    }
33 PointedEar 137
 
35 PointedEar 138
    this.list();
31 PointedEar 139
 
35 PointedEar 140
    return result;
31 PointedEar 141
  }
142
 
143
  /**
35 PointedEar 144
   * deletes a term, gets the ID from the "id" parameter that was submitted with
31 PointedEar 145
   * the HTTP request
146
   *
147
   * @return String - either SUCCESS or ERROR constant
148
   */
149
  public String delete()
150
  {
151
 
152
    int id = this.getIdParameter();
153
 
154
    /* Check for malicious ID values */
35 PointedEar 155
    String result = Action.SUCCESS;
31 PointedEar 156
    if (id > 0)
157
    {
158
      this.termDAO.deleteTerm(id);
159
    }
160
    else
161
    {
35 PointedEar 162
      result = Action.ERROR;
31 PointedEar 163
    }
35 PointedEar 164
 
165
    this.list();
166
 
167
    return result;
31 PointedEar 168
  }
169
 
35 PointedEar 170
  /**
171
   * Gets the ID Parameter for update / delete requests
172
   *
173
   * @return int from the ID request. If not set or wrong, it gives back -1
31 PointedEar 174
   */
35 PointedEar 175
  private int getIdParameter()
176
  {
177
    int id = -1;
178
    try
179
    {
180
      id = Integer.parseInt(this.request.getParameter("id")); //$NON-NLS-1$
181
    }
182
    catch (Exception e)
183
    {
184
      /* TODO: Logging - wrong parameter set */
185
    }
31 PointedEar 186
 
35 PointedEar 187
    return id;
188
  }
189
 
190
  /* Standard getters and setters */
191
 
31 PointedEar 192
  /**
193
   * @return The term edited with this instance
194
   */
195
  public Term getTerm()
196
  {
197
    return this.term;
198
  }
199
 
200
  /**
201
   * @param term
202
   *          The term edited with this instance
203
   */
204
  public void setTerm(Term term)
205
  {
206
    this.term = term;
207
  }
208
 
209
  /**
210
   * @return The list of terms edited with this instance
211
   */
212
  public List<Term> getTermList()
213
  {
214
    return this.termList;
215
  }
216
 
217
  /**
218
   * @param termList
219
   *          The list of terms edited with this instance
220
   */
221
  public void setTermList(List<Term> termList)
222
  {
223
    this.termList = termList;
224
  }
225
}