View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.engine.statement;
28  
29  import com.rapiddweller.benerator.Generator;
30  import com.rapiddweller.benerator.engine.BeneratorContext;
31  import com.rapiddweller.benerator.wrapper.ProductWrapper;
32  import com.rapiddweller.common.Context;
33  import com.rapiddweller.common.ErrorHandler;
34  import com.rapiddweller.common.IOUtil;
35  import com.rapiddweller.script.Expression;
36  import com.rapiddweller.task.PageListener;
37  import com.rapiddweller.task.TaskExecutor;
38  
39  import java.io.Closeable;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * Creates a number of entities in parallel execution and a given page size.<br/><br/>
45   * Created: 01.02.2008 14:43:15
46   *
47   * @author Volker Bergmann
48   */
49  public class GenerateOrIterateStatement extends AbstractStatement implements Closeable, PageListener {
50  
51    /**
52     * The Task.
53     */
54    protected GenerateAndConsumeTask task;
55    /**
56     * The Count generator.
57     */
58    protected final Generator<Long> countGenerator;
59    /**
60     * The Min count.
61     */
62    protected final Expression<Long> minCount;
63    /**
64     * The Page size.
65     */
66    protected final Expression<Long> pageSize;
67    /**
68     * The Page listener ex.
69     */
70    protected final Expression<PageListener> pageListenerEx;
71    /**
72     * The Page listener.
73     */
74    protected PageListener pageListener;
75    /**
76     * The Info log.
77     */
78    protected final boolean infoLog;
79    /**
80     * The Is sub creator.
81     */
82    protected final boolean isSubCreator;
83    /**
84     * The Context.
85     */
86    protected final BeneratorContext context;
87    /**
88     * The Child context.
89     */
90    protected final BeneratorContext childContext;
91  
92    /**
93     * Instantiates a new Generate or iterate statement.
94     *
95     * @param productName    the product name
96     * @param countGenerator the count generator
97     * @param minCount       the min count
98     * @param pageSize       the page size
99     * @param pageListenerEx the page listener ex
100    * @param errorHandler   the error handler
101    * @param infoLog        the info log
102    * @param isSubCreator   the is sub creator
103    * @param context        the context
104    */
105   public GenerateOrIterateStatement(String productName, Generator<Long> countGenerator, Expression<Long> minCount,
106                                     Expression<Long> pageSize, Expression<PageListener> pageListenerEx,
107                                     Expression<ErrorHandler> errorHandler, boolean infoLog, boolean isSubCreator, BeneratorContext context) {
108     this.task = null;
109     this.countGenerator = countGenerator;
110     this.minCount = minCount;
111     this.pageSize = pageSize;
112     this.pageListenerEx = pageListenerEx;
113     this.infoLog = infoLog;
114     this.isSubCreator = isSubCreator;
115     this.context = context;
116     this.childContext = context.createSubContext(productName);
117   }
118 
119   /**
120    * Sets task.
121    *
122    * @param task the task
123    */
124   public void setTask(GenerateAndConsumeTask task) {
125     this.task = task;
126   }
127 
128   /**
129    * Gets task.
130    *
131    * @return the task
132    */
133   public GenerateAndConsumeTask getTask() {
134     return task;
135   }
136 
137   /**
138    * Gets context.
139    *
140    * @return the context
141    */
142   public BeneratorContext getContext() {
143     return context;
144   }
145 
146   /**
147    * Gets child context.
148    *
149    * @return the child context
150    */
151   public BeneratorContext getChildContext() {
152     return childContext;
153   }
154 
155 
156   // Statement interface ---------------------------------------------------------------------------------------------
157 
158   @Override
159   public boolean execute(BeneratorContext ctx) {
160     if (!beInitialized(ctx)) {
161       task.reset();
162     }
163     executeTask(generateCount(childContext), minCount.evaluate(childContext), pageSize.evaluate(childContext),
164         evaluatePageListeners(childContext), getErrorHandler(childContext));
165     if (!isSubCreator) {
166       close();
167     }
168     return true;
169   }
170 
171   /**
172    * Generate count long.
173    *
174    * @param context the context
175    * @return the long
176    */
177   public Long generateCount(BeneratorContext context) {
178     beInitialized(context);
179     ProductWrapper<Long> count = countGenerator.generate(new ProductWrapper<>());
180     return (count != null ? count.unwrap() : null);
181   }
182 
183   @Override
184   public void close() {
185     task.close();
186     countGenerator.close();
187     if (pageListener instanceof Closeable) {
188       IOUtil.close((Closeable) pageListener);
189     }
190   }
191 
192   // PageListener interface implementation ---------------------------------------------------------------------------
193 
194   @Override
195   public void pageStarting() {
196   }
197 
198   @Override
199   public void pageFinished() {
200     getTask().pageFinished();
201   }
202 
203 
204   // internal helpers ------------------------------------------------------------------------------------------------
205 
206   /**
207    * Evaluate page listeners list.
208    *
209    * @param context the context
210    * @return the list
211    */
212   protected List<PageListener> evaluatePageListeners(Context context) {
213     List<PageListener> listeners = new ArrayList<>();
214     if (pageListener != null) {
215       pageListener = pageListenerEx.evaluate(context);
216       if (pageListener != null) {
217         listeners.add(pageListener);
218       }
219     }
220     return listeners;
221   }
222 
223   /**
224    * Be initialized boolean.
225    *
226    * @param context the context
227    * @return the boolean
228    */
229   protected boolean beInitialized(BeneratorContext context) {
230     if (!countGenerator.wasInitialized()) {
231       countGenerator.init(childContext);
232       task.init(childContext);
233       return true;
234     }
235     return false;
236   }
237 
238   /**
239    * Execute task.
240    *
241    * @param requestedExecutions the requested executions
242    * @param minExecutions       the min executions
243    * @param pageSizeValue       the page size value
244    * @param pageListeners       the page listeners
245    * @param errorHandler        the error handler
246    */
247   protected void executeTask(Long requestedExecutions, Long minExecutions, Long pageSizeValue,
248                              List<PageListener> pageListeners, ErrorHandler errorHandler) {
249     TaskExecutor.execute(task, childContext, requestedExecutions, minExecutions,
250         pageListeners, pageSizeValue, false, errorHandler, infoLog);
251   }
252 
253 }