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