001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * OrderLineTimeFactory.java
021 */
022
023package com.echothree.model.data.order.server.factory;
024
025import com.echothree.model.data.order.common.pk.OrderLinePK;
026import com.echothree.model.data.order.common.pk.OrderTimeTypePK;
027
028import com.echothree.model.data.order.server.entity.OrderLine;
029import com.echothree.model.data.order.server.entity.OrderTimeType;
030
031import com.echothree.model.data.order.common.OrderLineTimeConstants;
032import com.echothree.model.data.order.common.pk.OrderLineTimePK;
033import com.echothree.model.data.order.server.value.OrderLineTimeValue;
034import com.echothree.model.data.order.server.entity.OrderLineTime;
035import com.echothree.util.common.exception.PersistenceDatabaseException;
036import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
037import com.echothree.util.common.exception.PersistenceNotNullException;
038import com.echothree.util.server.persistence.BaseFactory;
039import com.echothree.util.server.persistence.EntityIdGenerator;
040import com.echothree.util.server.persistence.EntityPermission;
041import com.echothree.util.server.persistence.PersistenceDebugFlags;
042import com.echothree.util.server.persistence.Session;
043import com.echothree.util.server.persistence.ThreadSession;
044import java.sql.PreparedStatement;
045import java.sql.ResultSet;
046import java.sql.SQLException;
047import java.sql.Types;
048import java.io.ByteArrayInputStream;
049import java.io.StringReader;
050import java.util.ArrayList;
051import java.util.Collection;
052import java.util.HashSet;
053import java.util.List;
054import java.util.Map;
055import java.util.Set;
056import org.apache.commons.logging.Log;
057import org.apache.commons.logging.LogFactory;
058
059public class OrderLineTimeFactory
060        implements BaseFactory<OrderLineTimePK, OrderLineTime> {
061    
062    //final private static Log log = LogFactory.getLog(OrderLineTimeFactory.class);
063    
064    final private static String SQL_SELECT_READ_ONLY = "SELECT ordltim_orderlinetimeid, ordltim_ordl_orderlineid, ordltim_ordtimtyp_ordertimetypeid, ordltim_time, ordltim_fromtime, ordltim_thrutime FROM orderlinetimes WHERE ordltim_orderlinetimeid = ?";
065    final private static String SQL_SELECT_READ_WRITE = "SELECT ordltim_orderlinetimeid, ordltim_ordl_orderlineid, ordltim_ordtimtyp_ordertimetypeid, ordltim_time, ordltim_fromtime, ordltim_thrutime FROM orderlinetimes WHERE ordltim_orderlinetimeid = ? FOR UPDATE";
066    final private static String SQL_INSERT = "INSERT INTO orderlinetimes (ordltim_orderlinetimeid, ordltim_ordl_orderlineid, ordltim_ordtimtyp_ordertimetypeid, ordltim_time, ordltim_fromtime, ordltim_thrutime) VALUES (?, ?, ?, ?, ?, ?)";
067    final private static String SQL_UPDATE = "UPDATE orderlinetimes SET ordltim_ordl_orderlineid = ?, ordltim_ordtimtyp_ordertimetypeid = ?, ordltim_time = ?, ordltim_fromtime = ?, ordltim_thrutime = ? WHERE ordltim_orderlinetimeid = ?";
068    final private static String SQL_DELETE = "DELETE FROM orderlinetimes WHERE ordltim_orderlinetimeid = ?";
069    final private static String SQL_VALID = "SELECT COUNT(*) FROM orderlinetimes WHERE ordltim_orderlinetimeid = ?";
070    
071    final private static String PK_COLUMN = "ordltim_orderlinetimeid";
072    final private static String ALL_COLUMNS = "ordltim_orderlinetimeid, ordltim_ordl_orderlineid, ordltim_ordtimtyp_ordertimetypeid, ordltim_time, ordltim_fromtime, ordltim_thrutime";
073    final public static String TABLE_NAME = "orderlinetimes";
074    
075    final public static String ORDLTIM_ORDERLINETIMEID = "ordltim_orderlinetimeid";
076    final public static String ORDLTIM_ORDL_ORDERLINEID = "ordltim_ordl_orderlineid";
077    final public static String ORDLTIM_ORDTIMTYP_ORDERTIMETYPEID = "ordltim_ordtimtyp_ordertimetypeid";
078    final public static String ORDLTIM_TIME = "ordltim_time";
079    final public static String ORDLTIM_FROMTIME = "ordltim_fromtime";
080    final public static String ORDLTIM_THRUTIME = "ordltim_thrutime";
081    
082    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(OrderLineTimeConstants.COMPONENT_VENDOR_NAME, OrderLineTimeConstants.ENTITY_TYPE_NAME);
083    
084    /** Creates a new instance of OrderLineTimeFactory */
085    private OrderLineTimeFactory() {
086        super();
087    }
088    
089    private static class OrderLineTimeFactoryHolder {
090        static OrderLineTimeFactory instance = new OrderLineTimeFactory();
091    }
092    
093    public static OrderLineTimeFactory getInstance() {
094        return OrderLineTimeFactoryHolder.instance;
095    }
096    
097    @Override
098    public String getPKColumn() {
099        return PK_COLUMN;
100    }
101    
102    @Override
103    public String getAllColumns() {
104        return ALL_COLUMNS;
105    }
106    
107    @Override
108    public String getTableName() {
109        return TABLE_NAME;
110    }
111    
112    @Override
113    public String getComponentVendorName() {
114        return OrderLineTimeConstants.COMPONENT_VENDOR_NAME;
115    }
116    
117    @Override
118    public String getEntityTypeName() {
119        return OrderLineTimeConstants.ENTITY_TYPE_NAME;
120    }
121    
122    public PreparedStatement prepareStatement(String query) {
123        return ThreadSession.currentSession().prepareStatement(OrderLineTimeFactory.class, query);
124    }
125    
126    public OrderLineTimePK getNextPK() {
127        return new OrderLineTimePK(entityIdGenerator.getNextEntityId());
128    }
129    
130    public Set<OrderLineTimePK> getPKsFromResultSetAsSet(ResultSet rs)
131            throws PersistenceDatabaseException {
132        Set<OrderLineTimePK> _result = new HashSet<>();
133        
134        try {
135            while(rs.next()) {
136                _result.add(getPKFromResultSet(rs));
137            }
138        } catch (SQLException se) {
139            throw new PersistenceDatabaseException(se);
140        }
141        
142        return _result;
143    }
144    
145    public java.util.List<OrderLineTimePK> getPKsFromResultSetAsList(ResultSet rs)
146            throws PersistenceDatabaseException {
147        java.util.List<OrderLineTimePK> _result = new ArrayList<>();
148        
149        try {
150            while(rs.next()) {
151                _result.add(getPKFromResultSet(rs));
152            }
153        } catch (SQLException se) {
154            throw new PersistenceDatabaseException(se);
155        }
156        
157        return _result;
158    }
159    
160    public OrderLineTimePK getPKFromResultSet(ResultSet rs)
161            throws PersistenceDatabaseException {
162        OrderLineTimePK _result;
163        
164        try {
165            long ordltim_orderlinetimeid = rs.getLong(ORDLTIM_ORDERLINETIMEID);
166            Long _entityId = rs.wasNull() ? null : ordltim_orderlinetimeid;
167            
168            _result = new OrderLineTimePK(_entityId);
169        } catch (SQLException se) {
170            throw new PersistenceDatabaseException(se);
171        }
172        
173        return _result;
174    }
175    
176    public java.util.List<OrderLineTimeValue> getValuesFromPKs(Session session, Collection<OrderLineTimePK> pks)
177            throws PersistenceDatabaseException {
178        java.util.List<OrderLineTimeValue> _values = new ArrayList<>(pks.size());
179        
180        for(OrderLineTimePK _pk: pks) {
181            _values.add(getValueFromPK(session, _pk));
182        }
183        
184        return _values;
185    }
186    
187    public OrderLineTimeValue getValueFromPK(Session session, OrderLineTimePK pk)
188            throws PersistenceDatabaseException {
189        OrderLineTimeValue _value;
190        
191        // See if we already have the entity in the session cache
192        OrderLineTime _entity = (OrderLineTime)session.getEntity(pk);
193        if(_entity == null)
194            _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getOrderLineTimeValue();
195        else
196            _value = _entity.getOrderLineTimeValue();
197        
198        return _value;
199    }
200    
201    public java.util.List<OrderLineTimeValue> getValuesFromResultSet(Session session, ResultSet rs)
202            throws PersistenceDatabaseException {
203        java.util.List<OrderLineTimeValue> _result = new ArrayList<>();
204        
205        try {
206            while(rs.next()) {
207                _result.add(getValueFromResultSet(session, rs));
208            }
209        } catch (SQLException se) {
210            throw new PersistenceDatabaseException(se);
211        }
212        
213        return _result;
214    }
215    
216    public OrderLineTimeValue getValueFromResultSet(Session session, ResultSet rs)
217            throws PersistenceDatabaseException {
218        OrderLineTimeValue _value;
219        
220        try {
221            Long ordltim_orderlinetimeid = rs.getLong(ORDLTIM_ORDERLINETIMEID);
222            OrderLineTimePK _pk = new OrderLineTimePK(ordltim_orderlinetimeid);
223            
224            // See if we already have the entity in the session cache
225            OrderLineTime _entity = (OrderLineTime)session.getEntity(_pk);
226            
227            if(_entity == null) {
228                Long ordltim_ordl_orderlineid = rs.getLong(ORDLTIM_ORDL_ORDERLINEID);
229                if(rs.wasNull())
230                    ordltim_ordl_orderlineid = null;
231                
232                Long ordltim_ordtimtyp_ordertimetypeid = rs.getLong(ORDLTIM_ORDTIMTYP_ORDERTIMETYPEID);
233                if(rs.wasNull())
234                    ordltim_ordtimtyp_ordertimetypeid = null;
235                
236                Long ordltim_time = rs.getLong(ORDLTIM_TIME);
237                if(rs.wasNull())
238                    ordltim_time = null;
239                
240                Long ordltim_fromtime = rs.getLong(ORDLTIM_FROMTIME);
241                if(rs.wasNull())
242                    ordltim_fromtime = null;
243                
244                Long ordltim_thrutime = rs.getLong(ORDLTIM_THRUTIME);
245                if(rs.wasNull())
246                    ordltim_thrutime = null;
247                
248                _value = new OrderLineTimeValue(_pk, new OrderLinePK(ordltim_ordl_orderlineid), new OrderTimeTypePK(ordltim_ordtimtyp_ordertimetypeid), ordltim_time, ordltim_fromtime, ordltim_thrutime);
249            } else
250                _value = _entity.getOrderLineTimeValue();
251        } catch (SQLException se) {
252            throw new PersistenceDatabaseException(se);
253        }
254        
255        return _value;
256    }
257    
258    public java.util.List<OrderLineTime> getEntitiesFromPKs(EntityPermission entityPermission, Collection<OrderLineTimePK> pks)
259            throws PersistenceDatabaseException {
260        return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks);
261    }
262    
263    public java.util.List<OrderLineTime> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<OrderLineTimePK> pks)
264            throws PersistenceDatabaseException {
265        java.util.List<OrderLineTime> _entities = new ArrayList<>(pks.size());
266        
267        for(OrderLineTimePK _pk: pks) {
268            _entities.add(getEntityFromPK(session, entityPermission, _pk));
269        }
270        
271        return _entities;
272    }
273    
274    public OrderLineTime getEntityFromValue(EntityPermission entityPermission, OrderLineTimeValue value) {
275        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey());
276    }
277    
278    public OrderLineTime getEntityFromValue(Session session, EntityPermission entityPermission, OrderLineTimeValue value) {
279        return getEntityFromPK(session, entityPermission, value.getPrimaryKey());
280    }
281    
282    public OrderLineTime getEntityFromPK(EntityPermission entityPermission, OrderLineTimePK pk)
283            throws PersistenceDatabaseException {
284        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk);
285    }
286    
287    public OrderLineTime getEntityFromCache(Session session, OrderLineTimePK pk) {
288        OrderLineTimeValue _value = (OrderLineTimeValue)session.getValueCache().get(pk);
289    
290        return _value == null ? null : new OrderLineTime(_value, EntityPermission.READ_ONLY);
291    }
292    
293    public OrderLineTime getEntityFromPK(Session session, EntityPermission entityPermission, OrderLineTimePK pk)
294            throws PersistenceDatabaseException {
295        OrderLineTime _entity;
296        
297        // See if we already have the entity in the session cache
298        _entity = (OrderLineTime)session.getEntity(pk);
299        if(_entity != null) {
300            // If the requested permission is READ_WRITE, and the cached permission is
301            // READ_ONLY, then pretend that the cached object wasn't found, and create
302            // a new entity that is READ_WRITE.
303            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
304                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
305                    _entity = null;
306            }
307        }
308        
309        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
310            _entity = getEntityFromCache(session, pk);
311        }
312        
313        if(_entity == null) {
314            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
315            long _entityId = pk.getEntityId();
316            ResultSet _rs = null;
317            
318            try {
319                _ps.setLong(1, _entityId);
320                _rs = _ps.executeQuery();
321                if(_rs.next()) {
322                    _entity = getEntityFromResultSet(session, entityPermission, _rs);
323                }
324            } catch (SQLException se) {
325                throw new PersistenceDatabaseException(se);
326            } finally {
327                if(_rs != null) {
328                    try {
329                        _rs.close();
330                    } catch (SQLException se) {
331                        // do nothing
332                    }
333                }
334            }
335        }
336        
337        return _entity;
338    }
339    
340    public Set<OrderLineTimePK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
341            throws PersistenceDatabaseException {
342        Set<OrderLineTimePK> _pks;
343        ResultSet _rs = null;
344        
345        try {
346            if(params.length != 0) {
347                Session.setQueryParams(ps, params);
348            }
349            
350            _rs = ps.executeQuery();
351            _pks = getPKsFromResultSetAsSet(_rs);
352            _rs.close();
353        } catch (SQLException se) {
354            throw new PersistenceDatabaseException(se);
355        } finally {
356            if(_rs != null) {
357                try {
358                    _rs.close();
359                } catch (SQLException se) {
360                    // do nothing
361                }
362            }
363        }
364        
365        return _pks;
366    }
367    
368    public java.util.List<OrderLineTimePK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
369            throws PersistenceDatabaseException {
370        java.util.List<OrderLineTimePK> _pks;
371        ResultSet _rs = null;
372        
373        try {
374            if(params.length != 0) {
375                Session.setQueryParams(ps, params);
376            }
377            
378            _rs = ps.executeQuery();
379            _pks = getPKsFromResultSetAsList(_rs);
380            _rs.close();
381        } catch (SQLException se) {
382            throw new PersistenceDatabaseException(se);
383        } finally {
384            if(_rs != null) {
385                try {
386                    _rs.close();
387                } catch (SQLException se) {
388                    // do nothing
389                }
390            }
391        }
392        
393        return _pks;
394    }
395    
396    public OrderLineTimePK getPKFromQuery(PreparedStatement ps, final Object... params)
397            throws PersistenceDatabaseException {
398        OrderLineTimePK _pk = null;
399        ResultSet _rs = null;
400        
401        try {
402            if(params.length != 0) {
403                Session.setQueryParams(ps, params);
404            }
405            
406            _rs = ps.executeQuery();
407            if(_rs.next()) {
408                _pk = getPKFromResultSet(_rs);
409            }
410            _rs.close();
411        } catch (SQLException se) {
412            throw new PersistenceDatabaseException(se);
413        } finally {
414            if(_rs != null) {
415                try {
416                    _rs.close();
417                } catch (SQLException se) {
418                    // do nothing
419                }
420            }
421        }
422        
423        return _pk;
424    }
425    
426    public java.util.List<OrderLineTime> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
427            throws PersistenceDatabaseException {
428        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
429        
430        return getEntitiesFromQuery(session, entityPermission, ps, params);
431    }
432    
433    public java.util.List<OrderLineTime> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
434            throws PersistenceDatabaseException {
435        Session session = ThreadSession.currentSession();
436        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
437        
438        return getEntitiesFromQuery(session, entityPermission, ps, params);
439    }
440    
441    public java.util.List<OrderLineTime> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
442            throws PersistenceDatabaseException {
443        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
444        
445        return getEntitiesFromQuery(session, entityPermission, ps);
446    }
447    
448    public java.util.List<OrderLineTime> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
449            throws PersistenceDatabaseException {
450        Session session = ThreadSession.currentSession();
451        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
452        
453        return getEntitiesFromQuery(session, entityPermission, ps);
454    }
455    
456    public java.util.List<OrderLineTime> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps)
457            throws PersistenceDatabaseException {
458        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps);
459    }
460    
461    public java.util.List<OrderLineTime> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
462            throws PersistenceDatabaseException {
463        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
464    }
465    
466    public java.util.List<OrderLineTime> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
467            throws PersistenceDatabaseException {
468        java.util.List<OrderLineTime> _entities;
469        ResultSet _rs = null;
470        
471        try {
472            if(params.length != 0) {
473                Session.setQueryParams(ps, params);
474            }
475            
476            _rs = ps.executeQuery();
477            _entities = getEntitiesFromResultSet(session, entityPermission, _rs);
478            _rs.close();
479        } catch (SQLException se) {
480            throw new PersistenceDatabaseException(se);
481        } finally {
482            if(_rs != null) {
483                try {
484                    _rs.close();
485                } catch (SQLException se) {
486                    // do nothing
487                }
488            }
489        }
490        
491        return _entities;
492    }
493    
494    public OrderLineTime getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
495            throws PersistenceDatabaseException {
496        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
497        
498        return getEntityFromQuery(session, entityPermission, ps, params);
499    }
500    
501    public OrderLineTime getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
502            throws PersistenceDatabaseException {
503        Session session = ThreadSession.currentSession();
504        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
505        
506        return getEntityFromQuery(session, entityPermission, ps, params);
507    }
508    
509    public OrderLineTime getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
510            throws PersistenceDatabaseException {
511        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
512        
513        return getEntityFromQuery(session, entityPermission, ps);
514    }
515    
516    public OrderLineTime getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
517            throws PersistenceDatabaseException {
518        Session session = ThreadSession.currentSession();
519        PreparedStatement ps = session.prepareStatement(OrderLineTimeFactory.class, queryMap.get(entityPermission));
520        
521        return getEntityFromQuery(session, entityPermission, ps);
522    }
523    
524    public OrderLineTime getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps)
525            throws PersistenceDatabaseException {
526        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps);
527    }
528    
529    public OrderLineTime getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
530            throws PersistenceDatabaseException {
531        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
532    }
533    
534    public OrderLineTime getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
535            throws PersistenceDatabaseException {
536        OrderLineTime _entity = null;
537        ResultSet _rs = null;
538        
539        try {
540            if(params.length != 0) {
541                Session.setQueryParams(ps, params);
542            }
543            
544            _rs = ps.executeQuery();
545            if(_rs.next()) {
546                _entity = getEntityFromResultSet(session, entityPermission, _rs);
547            }
548            _rs.close();
549        } catch (SQLException se) {
550            throw new PersistenceDatabaseException(se);
551        } finally {
552            if(_rs != null) {
553                try {
554                    _rs.close();
555                } catch (SQLException se) {
556                    // do nothing
557                }
558            }
559        }
560        
561        return _entity;
562    }
563    
564    public java.util.List<OrderLineTime> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
565            throws PersistenceDatabaseException {
566        return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
567    }
568    
569    public java.util.List<OrderLineTime> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
570            throws PersistenceDatabaseException {
571        java.util.List<OrderLineTime> _result = new ArrayList<>();
572        
573        try {
574            while(rs.next()) {
575                _result.add(getEntityFromResultSet(session, entityPermission, rs));
576            }
577        } catch (SQLException se) {
578            throw new PersistenceDatabaseException(se);
579        }
580        
581        return _result;
582    }
583    
584    public OrderLineTime getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
585            throws PersistenceDatabaseException {
586        return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
587    }
588    
589    public OrderLineTime getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
590            throws PersistenceDatabaseException {
591        OrderLineTime _entity;
592        
593        try {
594            Long ordltim_orderlinetimeid = rs.getLong(ORDLTIM_ORDERLINETIMEID);
595            OrderLineTimePK _pk = new OrderLineTimePK(ordltim_orderlinetimeid);
596            
597            // See if we already have the entity in the session cache
598            _entity = (OrderLineTime)session.getEntity(_pk);
599            if(_entity != null) {
600                // If the requested permission is READ_WRITE, and the cached permission is
601                // READ_ONLY, then pretend that the cached object wasn't found, and create
602                // a new entity that is READ_WRITE.
603                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
604                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
605                        _entity = null;
606                }
607            }
608            boolean foundInSessionCache = _entity != null;
609            
610            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
611                _entity = getEntityFromCache(session, _pk);
612            }
613            
614            if(_entity == null) {
615                Long ordltim_ordl_orderlineid = rs.getLong(ORDLTIM_ORDL_ORDERLINEID);
616                if(rs.wasNull())
617                    ordltim_ordl_orderlineid = null;
618                
619                Long ordltim_ordtimtyp_ordertimetypeid = rs.getLong(ORDLTIM_ORDTIMTYP_ORDERTIMETYPEID);
620                if(rs.wasNull())
621                    ordltim_ordtimtyp_ordertimetypeid = null;
622                
623                Long ordltim_time = rs.getLong(ORDLTIM_TIME);
624                if(rs.wasNull())
625                    ordltim_time = null;
626                
627                Long ordltim_fromtime = rs.getLong(ORDLTIM_FROMTIME);
628                if(rs.wasNull())
629                    ordltim_fromtime = null;
630                
631                Long ordltim_thrutime = rs.getLong(ORDLTIM_THRUTIME);
632                if(rs.wasNull())
633                    ordltim_thrutime = null;
634                
635                OrderLineTimeValue _value = new OrderLineTimeValue(_pk, ordltim_ordl_orderlineid == null? null: new OrderLinePK(ordltim_ordl_orderlineid), ordltim_ordtimtyp_ordertimetypeid == null? null: new OrderTimeTypePK(ordltim_ordtimtyp_ordertimetypeid), ordltim_time, ordltim_fromtime, ordltim_thrutime);
636                _entity = new OrderLineTime(_value, entityPermission);
637            }
638            
639            if(!foundInSessionCache) {
640                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
641                    session.putReadOnlyEntity(_pk, _entity);
642                    session.getValueCache().put(_entity.getOrderLineTimeValue());
643                } else {
644                    session.putReadWriteEntity(_pk, _entity);
645                }
646            }
647        } catch (SQLException se) {
648            throw new PersistenceDatabaseException(se);
649        }
650        
651        return _entity;
652    }
653    
654    public OrderLineTime create(Session session, OrderLine orderLine, OrderTimeType orderTimeType, Long time, Long fromTime, Long thruTime)
655            throws PersistenceDatabaseException, PersistenceNotNullException {
656        return create(session, orderLine == null ? null : orderLine.getPrimaryKey(), orderTimeType == null ? null : orderTimeType.getPrimaryKey(), time, fromTime, thruTime);
657    }
658    
659    public OrderLineTime create(OrderLine orderLine, OrderTimeType orderTimeType, Long time, Long fromTime, Long thruTime)
660            throws PersistenceDatabaseException, PersistenceNotNullException {
661        return create(ThreadSession.currentSession(), orderLine == null ? null : orderLine.getPrimaryKey(), orderTimeType == null ? null : orderTimeType.getPrimaryKey(), time, fromTime, thruTime);
662    }
663    
664    private void bindForCreate(PreparedStatement _ps, OrderLineTimeValue _value)
665            throws SQLException {
666        _ps.setLong(1, _value.getEntityId());
667        
668        OrderLinePK ordltim_ordl_orderlineid = _value.getOrderLinePK();
669        if(ordltim_ordl_orderlineid == null)
670            _ps.setNull(2, Types.BIGINT);
671        else
672            _ps.setLong(2, ordltim_ordl_orderlineid.getEntityId());
673            
674        OrderTimeTypePK ordltim_ordtimtyp_ordertimetypeid = _value.getOrderTimeTypePK();
675        if(ordltim_ordtimtyp_ordertimetypeid == null)
676            _ps.setNull(3, Types.BIGINT);
677        else
678            _ps.setLong(3, ordltim_ordtimtyp_ordertimetypeid.getEntityId());
679            
680        Long ordltim_time = _value.getTime();
681        if(ordltim_time == null)
682            _ps.setNull(4, Types.BIGINT);
683        else
684            _ps.setLong(4, ordltim_time);
685            
686        Long ordltim_fromtime = _value.getFromTime();
687        if(ordltim_fromtime == null)
688            _ps.setNull(5, Types.BIGINT);
689        else
690            _ps.setLong(5, ordltim_fromtime);
691            
692        Long ordltim_thrutime = _value.getThruTime();
693        if(ordltim_thrutime == null)
694            _ps.setNull(6, Types.BIGINT);
695        else
696            _ps.setLong(6, ordltim_thrutime);
697            
698    }
699    
700    public OrderLineTime create(Session session, OrderLinePK orderLinePK, OrderTimeTypePK orderTimeTypePK, Long time, Long fromTime, Long thruTime)
701            throws PersistenceDatabaseException, PersistenceNotNullException {
702        OrderLineTimePK _pk = getNextPK();
703        OrderLineTimeValue _value = new OrderLineTimeValue(_pk, orderLinePK, orderTimeTypePK, time, fromTime, thruTime);
704        
705        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
706        
707        try {
708            bindForCreate(_ps, _value);
709            
710            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
711                int _count = _ps.executeUpdate();
712                
713                if(_count != 1) {
714                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
715                }
716            } else {
717                 _ps.executeUpdate();
718            }
719            
720            session.getValueCache().put(_value);
721        } catch (SQLException se) {
722            throw new PersistenceDatabaseException(se);
723        }
724        
725        OrderLineTime _entity = new OrderLineTime(_value, EntityPermission.READ_ONLY);
726        session.putReadOnlyEntity(_pk, _entity);
727        
728        return _entity;
729    }
730    
731    public OrderLineTime create(OrderLinePK orderLinePK, OrderTimeTypePK orderTimeTypePK, Long time, Long fromTime, Long thruTime)
732            throws PersistenceDatabaseException, PersistenceNotNullException {
733        return create(ThreadSession.currentSession(), orderLinePK, orderTimeTypePK, time, fromTime, thruTime);
734    }
735    
736    public void create(Session session, Collection<OrderLineTimeValue> _values)
737            throws PersistenceDatabaseException, PersistenceNotNullException {
738        int _size = _values.size();
739        
740        if(_size > 0) {
741            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
742            List<OrderLineTimeValue> _cacheValues = new ArrayList<>(_size);
743            
744            try {
745                for(OrderLineTimeValue _value : _values) {
746                    _value.setEntityId(entityIdGenerator.getNextEntityId());
747                    bindForCreate(_ps, _value);
748                    
749                    _ps.addBatch();
750                    
751                    _cacheValues.add(_value);
752                }
753                
754                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
755                    int[] _counts = _ps.executeBatch();
756                    
757                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
758                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
759                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
760                        }
761                    }
762                } else {
763                     _ps.executeBatch();
764                }
765                
766                _ps.clearBatch();
767            } catch (SQLException se) {
768                throw new PersistenceDatabaseException(se);
769            }
770            
771            _cacheValues.forEach((_cacheValue) -> {
772                OrderLineTime _cacheEntity = new OrderLineTime(_cacheValue, EntityPermission.READ_ONLY);
773                
774                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
775            });
776        }
777    }
778    
779    public void create(Collection<OrderLineTimeValue> _values)
780            throws PersistenceDatabaseException, PersistenceNotNullException {
781        create(ThreadSession.currentSession(), _values);
782    }
783    
784    private boolean bindForStore(PreparedStatement _ps, OrderLineTimeValue _value)
785            throws SQLException {
786        boolean _hasBeenModified = _value.hasBeenModified();
787        
788        if(_hasBeenModified) {
789            OrderLinePK ordltim_ordl_orderlineid = _value.getOrderLinePK();
790            if(ordltim_ordl_orderlineid == null)
791                _ps.setNull(1, Types.BIGINT);
792            else
793                _ps.setLong(1, ordltim_ordl_orderlineid.getEntityId());
794            
795            OrderTimeTypePK ordltim_ordtimtyp_ordertimetypeid = _value.getOrderTimeTypePK();
796            if(ordltim_ordtimtyp_ordertimetypeid == null)
797                _ps.setNull(2, Types.BIGINT);
798            else
799                _ps.setLong(2, ordltim_ordtimtyp_ordertimetypeid.getEntityId());
800            
801            Long ordltim_time = _value.getTime();
802            if(ordltim_time == null)
803                _ps.setNull(3, Types.BIGINT);
804            else
805                _ps.setLong(3, ordltim_time);
806            
807            Long ordltim_fromtime = _value.getFromTime();
808            if(ordltim_fromtime == null)
809                _ps.setNull(4, Types.BIGINT);
810            else
811                _ps.setLong(4, ordltim_fromtime);
812            
813            Long ordltim_thrutime = _value.getThruTime();
814            if(ordltim_thrutime == null)
815                _ps.setNull(5, Types.BIGINT);
816            else
817                _ps.setLong(5, ordltim_thrutime);
818            
819            _ps.setLong(6, _value.getPrimaryKey().getEntityId());
820            
821            _value.clearHasBeenModified();
822        }
823        
824        return _hasBeenModified;
825    }
826    
827    @Override
828    public void store(Session session, OrderLineTime entity)
829            throws PersistenceDatabaseException {
830        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
831        
832        try {
833            OrderLineTimeValue _value = entity.getOrderLineTimeValue();
834            
835            if(bindForStore(_ps, _value)) {
836                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
837                    int _count = _ps.executeUpdate();
838                    
839                    if(_count != 1) {
840                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
841                    }
842                } else {
843                     _ps.executeUpdate();
844                }
845                
846                session.getValueCache().put(_value);
847            }
848        } catch (SQLException se) {
849            throw new PersistenceDatabaseException(se);
850        }
851    }
852    
853    @Override
854    public void store(Session session, Collection<OrderLineTime> entities)
855            throws PersistenceDatabaseException {
856        if(entities.size() > 0) {
857            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
858            int _modifiedEntities = 0;
859            
860            try {
861                for(OrderLineTime entity : entities) {
862                    if(bindForStore(_ps, entity.getOrderLineTimeValue())) {
863                        _ps.addBatch();
864                        _modifiedEntities++;
865                    }
866                }
867                
868                if(_modifiedEntities != 0) {
869                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
870                        int[] _counts = _ps.executeBatch();
871                        
872                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
873                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
874                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
875                            }
876                        }
877                    } else {
878                         _ps.executeBatch();
879                    }
880                    
881                    _ps.clearBatch();
882                    
883                    entities.forEach((entity) -> {
884                        session.getValueCache().put(entity.getOrderLineTimeValue());
885                    });
886                }
887            } catch (SQLException se) {
888                throw new PersistenceDatabaseException(se);
889            }
890        }
891    }
892    
893    @Override
894    public void store(Collection<OrderLineTime> entities)
895            throws PersistenceDatabaseException {
896        store(ThreadSession.currentSession(), entities);
897    }
898    
899    @Override
900    public void remove(Session session, OrderLineTime entity)
901            throws PersistenceDatabaseException {
902        remove(session, entity.getPrimaryKey());
903    }
904    
905    @Override
906    public void remove(Session session, OrderLineTimePK pk)
907            throws PersistenceDatabaseException {
908        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
909        long _entityId = pk.getEntityId();
910        
911        try {
912            _ps.setLong(1, _entityId);
913            
914            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
915                int _count = _ps.executeUpdate();
916                
917                if(_count != 1) {
918                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
919                }
920            } else {
921                 _ps.executeUpdate();
922            }
923            
924            session.getValueCache().remove(pk);
925        } catch (SQLException se) {
926            throw new PersistenceDatabaseException(se);
927        }
928        
929        session.removed(pk, false);
930    }
931    
932    @Override
933    public void remove(Session session, Collection<OrderLineTimePK> pks)
934            throws PersistenceDatabaseException {
935        if(pks.size() > 0) {
936            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
937            int _modifiedEntities = 0;
938            
939            try {
940                for(OrderLineTimePK pk : pks) {
941                    long _entityId = pk.getEntityId();
942                    
943                    _ps.setLong(1, _entityId);
944                    
945                    _ps.addBatch();
946                    _modifiedEntities++;
947                }
948                
949                if(_modifiedEntities != 0) {
950                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
951                        int[] _counts = _ps.executeBatch();
952                        
953                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
954                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
955                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
956                            }
957                        }
958                    } else {
959                        _ps.executeBatch();
960                    }
961                    
962                    _ps.clearBatch();
963                    
964                    pks.forEach((pk) -> {
965                        session.getValueCache().remove(pk);
966                    });
967                }
968            } catch (SQLException se) {
969                throw new PersistenceDatabaseException(se);
970            }
971            
972            pks.forEach((pk) -> {
973                session.removed(pk, true);
974            });
975        }
976    }
977    
978    @Override
979    public void remove(Collection<OrderLineTimePK> pks)
980            throws PersistenceDatabaseException {
981        remove(ThreadSession.currentSession(), pks);
982    }
983    
984    @Override
985    public boolean validPK(Session session, OrderLineTimePK pk)
986            throws PersistenceDatabaseException {
987        boolean valid = false;
988        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
989        ResultSet _rs = null;
990        
991        try {
992            _ps.setLong(1, pk.getEntityId());
993            
994            _rs = _ps.executeQuery();
995            if(_rs.next()) {
996                long _count = _rs.getLong(1);
997                if(_rs.wasNull())
998                    _count = 0;
999                
1000                if(_count == 1)
1001                    valid = true;
1002            }
1003        } catch (SQLException se) {
1004            throw new PersistenceDatabaseException(se);
1005        } finally {
1006            if(_rs != null) {
1007                try {
1008                    _rs.close();
1009                } catch (SQLException se) {
1010                    // do nothing
1011                }
1012            }
1013        }
1014        
1015        return valid;
1016    }
1017    
1018}