Home » struts-2.1.8.1-src » org.apache » struts2 » showcase » jsf » [javadoc | source]

    1   /*
    2    * $Id: JsfEmployeeAction.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   package org.apache.struts2.showcase.jsf;
   22   
   23   import java.util.ArrayList;
   24   import java.util.Collection;
   25   import java.util.HashMap;
   26   import java.util.LinkedHashMap;
   27   import java.util.List;
   28   import java.util.Map;
   29   
   30   import org.apache.struts2.showcase.action.EmployeeAction;
   31   import org.apache.struts2.showcase.dao.SkillDao;
   32   import org.apache.struts2.showcase.model.Employee;
   33   import org.apache.struts2.showcase.model.Skill;
   34   
   35   /**
   36    * Overriding the EmployeeAction to main provide getters returning the data in
   37    * the form required by the JSF components
   38    */
   39   public class JsfEmployeeAction extends EmployeeAction {
   40   
   41       private static final long serialVersionUID = 1L;
   42   
   43       /**
   44        * Creating a default employee and main skill, since the JSF EL can't handle
   45        * creating new objects as necessary
   46        *
   47        */
   48       public JsfEmployeeAction() {
   49           Employee e = new Employee();
   50           e.setMainSkill(new Skill());
   51           setCurrentEmployee(e);
   52       }
   53   
   54       private SkillDao skillDao;
   55   
   56       public void setSkillDao(SkillDao skillDao) {
   57           this.skillDao = skillDao;
   58       }
   59   
   60       /**
   61        * Returning a List because the JSF dataGrid can't handle a Set for some
   62        * reason
   63        */
   64       @Override
   65       public Collection getAvailableItems() {
   66           return new ArrayList(super.getAvailableItems());
   67       }
   68   
   69       /**
   70        * Changing the String array into a Map
   71        */
   72       public Map<String, String> getAvailablePositionsAsMap() {
   73           Map<String, String> map = new LinkedHashMap<String, String>();
   74           for (String val : super.getAvailablePositions()) {
   75               map.put(val, val);
   76           }
   77           return map;
   78       }
   79   
   80       /**
   81        * Converting the list into a map
   82        */
   83       public Map getAvailableLevelsAsMap() {
   84           Map map = new LinkedHashMap();
   85           for (Object val : super.getAvailableLevels()) {
   86               map.put(val, val);
   87           }
   88           return map;
   89       }
   90   
   91       /**
   92        * Converting the Skill object list into a map
   93        */
   94       public Map<String, String> getAvailableSkills() {
   95           Map<String, String> map = new HashMap<String, String>();
   96           for (Object val : skillDao.findAll()) {
   97               Skill skill = (Skill) val;
   98               map.put(skill.getDescription(), skill.getName());
   99           }
  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   }

Home » struts-2.1.8.1-src » org.apache » struts2 » showcase » jsf » [javadoc | source]