KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > models > IterativeSubModel


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: IterativeSubModel.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.models;
21
22 import org.enhydra.barracuda.core.comp.*;
23 import org.apache.log4j.*;
24 import java.util.*;
25
26 /**
27  * Assume a "supermodel" containing a List of Lists (a 2 dimensional structure).
28  * An outer ("enclosing") model iterates over the the 1st dimension. The IterativeSubModel
29  * asks the enclosing model for its current value and iterates over the 2nd dimension
30  * @author Stefan Armbruster
31  * @version $Id: IterativeSubModel.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
32  */

33 public class IterativeSubModel extends IteratorModel {
34
35     protected static Logger logger = Logger.getLogger(IterativeSubModel.class);
36
37     /** reference to enclosing model */
38     protected TemplateModel enclosing;
39     protected String JavaDoc key;
40
41     /** initialize the model
42      * @param name Name of the model
43      * @param enclosing reference to surrounding model
44      */

45     public IterativeSubModel(String JavaDoc name, String JavaDoc key, TemplateModel enclosing) {
46         super(name);
47         this.enclosing = enclosing;
48         this.key = key;
49     }
50
51     public void preIterate() {
52         if (enclosing==null) {
53             logger.error("you didn't specify a valid enclosing model");
54             return;
55         }
56         Object JavaDoc referenceMain = enclosing.getItem(new TemplateDirective(null, null, key, null));
57         /*if (referenceMain == null) {
58             logger.error("the enclosing model (type: " + enclosing.getClass().getName() + ") doesn't have a " + key + " result for getItem");
59             return;
60         }*/

61
62         // if enclosing model delivers a List, use it. Otherwise, get a list out of the reference by calling getListFromReference
63
Collection coll = null;
64         if (referenceMain instanceof Collection) {
65             logger.debug("List: " + (Collection)referenceMain);
66             coll = (Collection)referenceMain;
67         } else {
68             logger.info("the enclosing model (type: " + enclosing.getClass().getName() + ") doesn't deliver a Collection, so call getListFromReference");
69             coll = getListFromReference(referenceMain);
70         }
71         if (coll == null) {
72             logger.error("could not get a list from model " + enclosing.getClass().getName());
73         } else {
74             setIterator( coll.iterator() );
75         }
76         super.preIterate();
77     }
78
79     /** dummy implementation, to be overridden in subclasses */
80     protected Collection getListFromReference(Object JavaDoc ref) {
81         throw new UnsupportedOperationException JavaDoc("getListFromReference is not yet implemented");
82     }
83
84     public Object JavaDoc getItem(String JavaDoc key) {
85         logger.debug("getting key : " + key);
86         return _current;
87     }
88 }
89
Popular Tags