001: /*
002: * $Id: JsfEmployeeAction.java 471756 2006-11-06 15:01:43Z husted $
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.jsf;
022:
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.LinkedHashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.struts2.showcase.action.EmployeeAction;
031: import org.apache.struts2.showcase.dao.SkillDao;
032: import org.apache.struts2.showcase.model.Employee;
033: import org.apache.struts2.showcase.model.Skill;
034:
035: /**
036: * Overriding the EmployeeAction to main provide getters returning the data in
037: * the form required by the JSF components
038: */
039: public class JsfEmployeeAction extends EmployeeAction {
040:
041: private static final long serialVersionUID = 1L;
042:
043: /**
044: * Creating a default employee and main skill, since the JSF EL can't handle
045: * creating new objects as necessary
046: *
047: */
048: public JsfEmployeeAction() {
049: Employee e = new Employee();
050: e.setMainSkill(new Skill());
051: setCurrentEmployee(e);
052: }
053:
054: private SkillDao skillDao;
055:
056: public void setSkillDao(SkillDao skillDao) {
057: this .skillDao = skillDao;
058: }
059:
060: /**
061: * Returning a List because the JSF dataGrid can't handle a Set for some
062: * reason
063: */
064: @Override
065: public Collection getAvailableItems() {
066: return new ArrayList(super .getAvailableItems());
067: }
068:
069: /**
070: * Changing the String array into a Map
071: */
072: public Map<String, String> getAvailablePositionsAsMap() {
073: Map<String, String> map = new LinkedHashMap<String, String>();
074: for (String val : super .getAvailablePositions()) {
075: map.put(val, val);
076: }
077: return map;
078: }
079:
080: /**
081: * Converting the list into a map
082: */
083: public Map getAvailableLevelsAsMap() {
084: Map map = new LinkedHashMap();
085: for (Object val : super .getAvailableLevels()) {
086: map.put(val, val);
087: }
088: return map;
089: }
090:
091: /**
092: * Converting the Skill object list into a map
093: */
094: public Map<String, String> getAvailableSkills() {
095: Map<String, String> map = new HashMap<String, String>();
096: for (Object val : skillDao.findAll()) {
097: Skill skill = (Skill) val;
098: map.put(skill.getDescription(), skill.getName());
099: }
100: return map;
101: }
102:
103: /**
104: * Gets the selected Skill objects as a list
105: */
106: public List<String> getSelectedSkillsAsList() {
107: System.out.println("asked for skills");
108: List<String> list = new ArrayList<String>();
109: List skills = super .getSelectedSkills();
110: if (skills != null) {
111: for (Object val : skills) {
112: if (val instanceof Skill) {
113: list.add(((Skill) val).getDescription());
114: } else {
115: Skill skill = skillDao.getSkill((String) val);
116: list.add(skill.getDescription());
117: }
118: }
119: }
120: return list;
121: }
122: }
|