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