Coverage Report - br.com.arsmachina.tapestrycrud.selectmodel.DefaultSingleTypeSelectModelFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultSingleTypeSelectModelFactory
0%
0/15
0%
0/8
0
 
 1  
 // Copyright 2008 Thiago H. de Paula Figueiredo
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package br.com.arsmachina.tapestrycrud.selectmodel;
 16  
 
 17  
 import java.util.ArrayList;
 18  
 import java.util.List;
 19  
 
 20  
 import org.apache.tapestry5.OptionModel;
 21  
 import org.apache.tapestry5.SelectModel;
 22  
 
 23  
 import br.com.arsmachina.controller.Controller;
 24  
 import br.com.arsmachina.tapestrycrud.encoder.LabelEncoder;
 25  
 
 26  
 /**
 27  
  * Default {@link SingleTypeSelectModelFactory} implementation for entity classes. It uses
 28  
  * {@link Controller#}
 29  
  * 
 30  
  * @author Thiago H. de Paula Figueiredo
 31  
  * @param <T> the type related to this factory.
 32  
  */
 33  
 public class DefaultSingleTypeSelectModelFactory<T> implements SingleTypeSelectModelFactory<T> {
 34  
 
 35  
         private Controller<T, ?> controller;
 36  
 
 37  
         private LabelEncoder<T> labelEncoder;
 38  
 
 39  
         /**
 40  
          * Single constructor of this class.
 41  
          * 
 42  
          * @param controller a {@link Controller}. It cannot be null.
 43  
          * @param encoder a {@link LabelEncoder}. It cannot be null.
 44  
          */
 45  
         @SuppressWarnings("unchecked")
 46  0
         public DefaultSingleTypeSelectModelFactory(Controller<T, ?> controller,
 47  
                         LabelEncoder<T> labelEncoder) {
 48  
 
 49  0
                 if (controller == null) {
 50  0
                         throw new IllegalArgumentException("Parameter controller cannot be null");
 51  
                 }
 52  
 
 53  0
                 if (labelEncoder == null) {
 54  0
                         throw new IllegalArgumentException("Parameter labelEncoder cannot be null");
 55  
                 }
 56  
 
 57  0
                 this.controller = controller;
 58  0
                 this.labelEncoder = labelEncoder;
 59  
 
 60  0
         }
 61  
 
 62  
         public SelectModel create(List<T> objects) {
 63  
 
 64  0
                 if (objects == null) {
 65  0
                         objects = controller.findAll();
 66  
                 }
 67  
 
 68  0
                 List<OptionModel> options = new ArrayList<OptionModel>();
 69  
 
 70  0
                 for (T object : objects) {
 71  
 
 72  0
                         final String label = labelEncoder.toLabel(object);
 73  0
                         options.add(new SimpleOptionModel(object, label));
 74  
 
 75  
                 }
 76  
 
 77  0
                 return new SimpleSelectModel(options);
 78  
 
 79  
         }
 80  
 
 81  
 }