Subversion Repositories WebE

Rev

Rev 31 | Rev 34 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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