View Javadoc

1   package org.weda.workflow.impl;
2   
3   import java.util.ArrayList;
4   import java.util.EmptyStackException;
5   import java.util.List;
6   import java.util.Stack;
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.weda.action.ActionDescriptor;
10  import org.weda.workflow.Entry;
11  import org.weda.workflow.EntryContainer;
12  import org.weda.workflow.EntryContainerException;
13  import org.weda.workflow.EntryException;
14  
15  /**
16   *
17   * @author Mikhail Titov
18   */
19  public class BasicEntryContainer<T extends Entry> 
20          extends AbstractEntry 
21          implements EntryContainer<T>
22  {
23      
24      private List<T> entries = new ArrayList<T>();
25      
26      public void init() throws EntryException {
27          super.init();
28          try{
29              for (Entry entry: getEntries()){
30                  entry.setWorkflow(getWorkflow());
31                  entry.setContainer(this);
32                  entry.init();
33              }
34          }catch (EntryException e){
35              throw new EntryContainerException(
36                      String.format(
37                          "Can't initialize workflow entry container " +
38                          "(type: %s, name: %s)"
39                          , this.getClass().getName(), getName())
40                      , e);
41          }
42      }
43      
44      public boolean operate(Stack<String> expressions) 
45          throws EntryException
46      {
47          super.operate(expressions);
48          StringBuffer stack = null;
49          if (getContainer()==null){
50              stack = new StringBuffer();
51              for (int i=expressions.size()-1; i>=0; i--)
52                  stack.append("("+expressions.get(i)+")");            
53          }
54          try{
55              String expression=expressions.pop();
56              Entry[] matchedEntries = new Entry[3];
57              for (Entry entry: getEntries()){
58                  if (entry.matches(expression)){
59                      if (entry.isDefaultEntry()){
60                          matchedEntries[2]=entry;
61                      }else if (entry.isExactMatch()){
62                          matchedEntries[0]=entry;
63                          //break;
64                      }else{
65                          matchedEntries[1]=entry;
66                          //if (matchedEntries[0]!=null)
67                              //break;
68                      }
69                  }
70              }
71              boolean operated=false;
72              Stack<String> expressionsCopy = (Stack<String>)expressions.clone();
73              for (Entry entry: matchedEntries){
74                  if (entry!=null){
75                      expressions=(Stack<String>)expressionsCopy.clone();
76                      operated=entry.operate(expressions);
77                      if (operated)
78                          break;
79                  }
80              }
81              if (!operated && getContainer()==null)
82                  throw new EntryContainerException(
83                          String.format(
84                              "Can't find final entry for expression stack: %s"
85                              , stack.toString())
86                          );
87              return operated;
88          }catch (EmptyStackException e){
89              throw new EntryContainerException(
90                      String.format(
91                          "Can't get expression from stack for workflow entry " +
92                          "(type: %s, name: %s)."
93                          , this.getClass().getName(), getName())
94                      , e);
95          }
96      }
97  
98      public List<T> getEntries() {
99          return entries;
100     }
101     
102     public void addEntry(T entry){
103         entries.add(entry);
104     }
105 
106     protected void logOperate(String expression) {
107         if (entryLog.isDebugEnabled())
108             entryLog.debug(
109                 logShift +
110                 String.format(
111                     "Searching for final entry in entry " +
112                     "(class: %s, name: %s) with the expression (%s)"
113                     , this.getClass().getName(), getName(), expression)
114             );
115     }
116 
117     public T getEntryByName(String entryName) {
118         for (T entry: entries)
119             if (entry.getName().equals(entryName))
120                 return entry;
121         return null;
122     }
123 
124     public void merge(EntryContainer<T> container)
125         throws EntryContainerException 
126     {
127         for (T entry: container.getEntries()){
128             T selfEntry = getEntryByName(entry.getName());
129             if (selfEntry == null)
130                 addEntry(entry);
131             else {
132                 if (selfEntry instanceof EntryContainer){
133                     ((EntryContainer)selfEntry).merge((EntryContainer)entry);
134                 }else
135                     throw new EntryContainerException(
136                         String.format(
137                             "Merge error. Leaf entry with name %s and " +
138                             "type %s already exists"
139                             , selfEntry.getName(), entry.getClass().getName())
140                     );
141             }
142         }
143     }
144 
145 }