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