Subversion Repositories WebE

Rev

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