001: /*
002: * $Id: EmployeeAction.java 476710 2006-11-19 05:05:14Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.action;
022:
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.log4j.Logger;
029: import org.apache.struts2.showcase.application.TestDataProvider;
030: import org.apache.struts2.showcase.dao.Dao;
031: import org.apache.struts2.showcase.dao.EmployeeDao;
032: import org.apache.struts2.showcase.model.Employee;
033: import org.apache.struts2.showcase.model.Skill;
034:
035: import com.opensymphony.xwork2.Preparable;
036:
037: /**
038: * JsfEmployeeAction.
039: */
040:
041: public class EmployeeAction extends AbstractCRUDAction implements
042: Preparable {
043:
044: private static final long serialVersionUID = 7047317819789938957L;
045:
046: private static final Logger log = Logger
047: .getLogger(EmployeeAction.class);
048:
049: private Long empId;
050: protected EmployeeDao employeeDao;
051: private Employee currentEmployee;
052: private List selectedSkills;
053:
054: public Long getEmpId() {
055: return empId;
056: }
057:
058: public void setEmpId(Long empId) {
059: this .empId = empId;
060: }
061:
062: public Employee getCurrentEmployee() {
063: return currentEmployee;
064: }
065:
066: public void setCurrentEmployee(Employee currentEmployee) {
067: this .currentEmployee = currentEmployee;
068: }
069:
070: public String[] getAvailablePositions() {
071: return TestDataProvider.POSITIONS;
072: }
073:
074: public List getAvailableLevels() {
075: return Arrays.asList(TestDataProvider.LEVELS);
076: }
077:
078: public List getSelectedSkills() {
079: return selectedSkills;
080: }
081:
082: public void setSelectedSkills(List selectedSkills) {
083: this .selectedSkills = selectedSkills;
084: }
085:
086: protected Dao getDao() {
087: return employeeDao;
088: }
089:
090: public void setEmployeeDao(EmployeeDao employeeDao) {
091: if (log.isDebugEnabled()) {
092: log
093: .debug("JsfEmployeeAction - [setEmployeeDao]: employeeDao injected.");
094: }
095: this .employeeDao = employeeDao;
096: }
097:
098: /**
099: * This method is called to allow the action to prepare itself.
100: *
101: * @throws Exception thrown if a system level exception occurs.
102: */
103: public void prepare() throws Exception {
104: Employee preFetched = (Employee) fetch(getEmpId(),
105: getCurrentEmployee());
106: if (preFetched != null) {
107: setCurrentEmployee(preFetched);
108: }
109: }
110:
111: public String execute() throws Exception {
112: if (getCurrentEmployee() != null
113: && getCurrentEmployee().getOtherSkills() != null) {
114: setSelectedSkills(new ArrayList());
115: Iterator it = getCurrentEmployee().getOtherSkills()
116: .iterator();
117: while (it.hasNext()) {
118: getSelectedSkills().add(((Skill) it.next()).getName());
119: }
120: }
121: return super .execute();
122: }
123:
124: public String save() throws Exception {
125: if (getCurrentEmployee() != null) {
126: setEmpId((Long) employeeDao.merge(getCurrentEmployee()));
127: employeeDao.setSkills(getEmpId(), getSelectedSkills());
128: }
129: return SUCCESS;
130: }
131:
132: }
|