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 * CustomerFactory.java
021 */
022
023package com.echothree.model.data.customer.server.factory;
024
025import com.echothree.model.data.party.common.pk.PartyPK;
026import com.echothree.model.data.customer.common.pk.CustomerTypePK;
027import com.echothree.model.data.offer.common.pk.OfferUsePK;
028import com.echothree.model.data.cancellationpolicy.common.pk.CancellationPolicyPK;
029import com.echothree.model.data.returnpolicy.common.pk.ReturnPolicyPK;
030import com.echothree.model.data.accounting.common.pk.GlAccountPK;
031
032import com.echothree.model.data.party.server.entity.Party;
033import com.echothree.model.data.customer.server.entity.CustomerType;
034import com.echothree.model.data.offer.server.entity.OfferUse;
035import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy;
036import com.echothree.model.data.returnpolicy.server.entity.ReturnPolicy;
037import com.echothree.model.data.accounting.server.entity.GlAccount;
038
039import com.echothree.model.data.customer.common.CustomerConstants;
040import com.echothree.model.data.customer.common.pk.CustomerPK;
041import com.echothree.model.data.customer.server.value.CustomerValue;
042import com.echothree.model.data.customer.server.entity.Customer;
043import com.echothree.util.common.exception.PersistenceDatabaseException;
044import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
045import com.echothree.util.common.exception.PersistenceNotNullException;
046import com.echothree.util.server.persistence.BaseFactory;
047import com.echothree.util.server.persistence.EntityIdGenerator;
048import com.echothree.util.server.persistence.EntityPermission;
049import com.echothree.util.server.persistence.PersistenceDebugFlags;
050import com.echothree.util.server.persistence.Session;
051import com.echothree.util.server.persistence.ThreadSession;
052import java.sql.PreparedStatement;
053import java.sql.ResultSet;
054import java.sql.SQLException;
055import java.sql.Types;
056import java.io.ByteArrayInputStream;
057import java.io.StringReader;
058import java.util.ArrayList;
059import java.util.Collection;
060import java.util.HashSet;
061import java.util.List;
062import java.util.Map;
063import java.util.Set;
064import org.apache.commons.logging.Log;
065import org.apache.commons.logging.LogFactory;
066
067public class CustomerFactory
068        implements BaseFactory<CustomerPK, Customer> {
069    
070    //final private static Log log = LogFactory.getLog(CustomerFactory.class);
071    
072    final private static String SQL_SELECT_READ_ONLY = "SELECT cu_customerid, cu_par_partyid, cu_customername, cu_cuty_customertypeid, cu_initialofferuseid, cu_cnclplcy_cancellationpolicyid, cu_rtnplcy_returnpolicyid, cu_arglaccountid, cu_holduntilcomplete, cu_allowbackorders, cu_allowsubstitutions, cu_allowcombiningshipments, cu_requirereference, cu_allowreferenceduplicates, cu_referencevalidationpattern, cu_fromtime, cu_thrutime FROM customers WHERE cu_customerid = ?";
073    final private static String SQL_SELECT_READ_WRITE = "SELECT cu_customerid, cu_par_partyid, cu_customername, cu_cuty_customertypeid, cu_initialofferuseid, cu_cnclplcy_cancellationpolicyid, cu_rtnplcy_returnpolicyid, cu_arglaccountid, cu_holduntilcomplete, cu_allowbackorders, cu_allowsubstitutions, cu_allowcombiningshipments, cu_requirereference, cu_allowreferenceduplicates, cu_referencevalidationpattern, cu_fromtime, cu_thrutime FROM customers WHERE cu_customerid = ? FOR UPDATE";
074    final private static String SQL_INSERT = "INSERT INTO customers (cu_customerid, cu_par_partyid, cu_customername, cu_cuty_customertypeid, cu_initialofferuseid, cu_cnclplcy_cancellationpolicyid, cu_rtnplcy_returnpolicyid, cu_arglaccountid, cu_holduntilcomplete, cu_allowbackorders, cu_allowsubstitutions, cu_allowcombiningshipments, cu_requirereference, cu_allowreferenceduplicates, cu_referencevalidationpattern, cu_fromtime, cu_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
075    final private static String SQL_UPDATE = "UPDATE customers SET cu_par_partyid = ?, cu_customername = ?, cu_cuty_customertypeid = ?, cu_initialofferuseid = ?, cu_cnclplcy_cancellationpolicyid = ?, cu_rtnplcy_returnpolicyid = ?, cu_arglaccountid = ?, cu_holduntilcomplete = ?, cu_allowbackorders = ?, cu_allowsubstitutions = ?, cu_allowcombiningshipments = ?, cu_requirereference = ?, cu_allowreferenceduplicates = ?, cu_referencevalidationpattern = ?, cu_fromtime = ?, cu_thrutime = ? WHERE cu_customerid = ?";
076    final private static String SQL_DELETE = "DELETE FROM customers WHERE cu_customerid = ?";
077    final private static String SQL_VALID = "SELECT COUNT(*) FROM customers WHERE cu_customerid = ?";
078    
079    final private static String PK_COLUMN = "cu_customerid";
080    final private static String ALL_COLUMNS = "cu_customerid, cu_par_partyid, cu_customername, cu_cuty_customertypeid, cu_initialofferuseid, cu_cnclplcy_cancellationpolicyid, cu_rtnplcy_returnpolicyid, cu_arglaccountid, cu_holduntilcomplete, cu_allowbackorders, cu_allowsubstitutions, cu_allowcombiningshipments, cu_requirereference, cu_allowreferenceduplicates, cu_referencevalidationpattern, cu_fromtime, cu_thrutime";
081    final public static String TABLE_NAME = "customers";
082    
083    final public static String CU_CUSTOMERID = "cu_customerid";
084    final public static String CU_PAR_PARTYID = "cu_par_partyid";
085    final public static String CU_CUSTOMERNAME = "cu_customername";
086    final public static String CU_CUTY_CUSTOMERTYPEID = "cu_cuty_customertypeid";
087    final public static String CU_INITIALOFFERUSEID = "cu_initialofferuseid";
088    final public static String CU_CNCLPLCY_CANCELLATIONPOLICYID = "cu_cnclplcy_cancellationpolicyid";
089    final public static String CU_RTNPLCY_RETURNPOLICYID = "cu_rtnplcy_returnpolicyid";
090    final public static String CU_ARGLACCOUNTID = "cu_arglaccountid";
091    final public static String CU_HOLDUNTILCOMPLETE = "cu_holduntilcomplete";
092    final public static String CU_ALLOWBACKORDERS = "cu_allowbackorders";
093    final public static String CU_ALLOWSUBSTITUTIONS = "cu_allowsubstitutions";
094    final public static String CU_ALLOWCOMBININGSHIPMENTS = "cu_allowcombiningshipments";
095    final public static String CU_REQUIREREFERENCE = "cu_requirereference";
096    final public static String CU_ALLOWREFERENCEDUPLICATES = "cu_allowreferenceduplicates";
097    final public static String CU_REFERENCEVALIDATIONPATTERN = "cu_referencevalidationpattern";
098    final public static String CU_FROMTIME = "cu_fromtime";
099    final public static String CU_THRUTIME = "cu_thrutime";
100    
101    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(CustomerConstants.COMPONENT_VENDOR_NAME, CustomerConstants.ENTITY_TYPE_NAME);
102    
103    /** Creates a new instance of CustomerFactory */
104    private CustomerFactory() {
105        super();
106    }
107    
108    private static class CustomerFactoryHolder {
109        static CustomerFactory instance = new CustomerFactory();
110    }
111    
112    public static CustomerFactory getInstance() {
113        return CustomerFactoryHolder.instance;
114    }
115    
116    @Override
117    public String getPKColumn() {
118        return PK_COLUMN;
119    }
120    
121    @Override
122    public String getAllColumns() {
123        return ALL_COLUMNS;
124    }
125    
126    @Override
127    public String getTableName() {
128        return TABLE_NAME;
129    }
130    
131    @Override
132    public String getComponentVendorName() {
133        return CustomerConstants.COMPONENT_VENDOR_NAME;
134    }
135    
136    @Override
137    public String getEntityTypeName() {
138        return CustomerConstants.ENTITY_TYPE_NAME;
139    }
140    
141    public PreparedStatement prepareStatement(String query) {
142        return ThreadSession.currentSession().prepareStatement(CustomerFactory.class, query);
143    }
144    
145    public CustomerPK getNextPK() {
146        return new CustomerPK(entityIdGenerator.getNextEntityId());
147    }
148    
149    public Set<CustomerPK> getPKsFromResultSetAsSet(ResultSet rs)
150            throws PersistenceDatabaseException {
151        Set<CustomerPK> _result = new HashSet<>();
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 java.util.List<CustomerPK> getPKsFromResultSetAsList(ResultSet rs)
165            throws PersistenceDatabaseException {
166        java.util.List<CustomerPK> _result = new ArrayList<>();
167        
168        try {
169            while(rs.next()) {
170                _result.add(getPKFromResultSet(rs));
171            }
172        } catch (SQLException se) {
173            throw new PersistenceDatabaseException(se);
174        }
175        
176        return _result;
177    }
178    
179    public CustomerPK getPKFromResultSet(ResultSet rs)
180            throws PersistenceDatabaseException {
181        CustomerPK _result;
182        
183        try {
184            long cu_customerid = rs.getLong(CU_CUSTOMERID);
185            Long _entityId = rs.wasNull() ? null : cu_customerid;
186            
187            _result = new CustomerPK(_entityId);
188        } catch (SQLException se) {
189            throw new PersistenceDatabaseException(se);
190        }
191        
192        return _result;
193    }
194    
195    public java.util.List<CustomerValue> getValuesFromPKs(Session session, Collection<CustomerPK> pks)
196            throws PersistenceDatabaseException {
197        java.util.List<CustomerValue> _values = new ArrayList<>(pks.size());
198        
199        for(CustomerPK _pk: pks) {
200            _values.add(getValueFromPK(session, _pk));
201        }
202        
203        return _values;
204    }
205    
206    public CustomerValue getValueFromPK(Session session, CustomerPK pk)
207            throws PersistenceDatabaseException {
208        CustomerValue _value;
209        
210        // See if we already have the entity in the session cache
211        Customer _entity = (Customer)session.getEntity(pk);
212        if(_entity == null)
213            _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getCustomerValue();
214        else
215            _value = _entity.getCustomerValue();
216        
217        return _value;
218    }
219    
220    public java.util.List<CustomerValue> getValuesFromResultSet(Session session, ResultSet rs)
221            throws PersistenceDatabaseException {
222        java.util.List<CustomerValue> _result = new ArrayList<>();
223        
224        try {
225            while(rs.next()) {
226                _result.add(getValueFromResultSet(session, rs));
227            }
228        } catch (SQLException se) {
229            throw new PersistenceDatabaseException(se);
230        }
231        
232        return _result;
233    }
234    
235    public CustomerValue getValueFromResultSet(Session session, ResultSet rs)
236            throws PersistenceDatabaseException {
237        CustomerValue _value;
238        
239        try {
240            Long cu_customerid = rs.getLong(CU_CUSTOMERID);
241            CustomerPK _pk = new CustomerPK(cu_customerid);
242            
243            // See if we already have the entity in the session cache
244            Customer _entity = (Customer)session.getEntity(_pk);
245            
246            if(_entity == null) {
247                Long cu_par_partyid = rs.getLong(CU_PAR_PARTYID);
248                if(rs.wasNull())
249                    cu_par_partyid = null;
250                
251                String cu_customername = rs.getString(CU_CUSTOMERNAME);
252                if(rs.wasNull())
253                    cu_customername = null;
254                
255                Long cu_cuty_customertypeid = rs.getLong(CU_CUTY_CUSTOMERTYPEID);
256                if(rs.wasNull())
257                    cu_cuty_customertypeid = null;
258                
259                Long cu_initialofferuseid = rs.getLong(CU_INITIALOFFERUSEID);
260                if(rs.wasNull())
261                    cu_initialofferuseid = null;
262                
263                Long cu_cnclplcy_cancellationpolicyid = rs.getLong(CU_CNCLPLCY_CANCELLATIONPOLICYID);
264                if(rs.wasNull())
265                    cu_cnclplcy_cancellationpolicyid = null;
266                
267                Long cu_rtnplcy_returnpolicyid = rs.getLong(CU_RTNPLCY_RETURNPOLICYID);
268                if(rs.wasNull())
269                    cu_rtnplcy_returnpolicyid = null;
270                
271                Long cu_arglaccountid = rs.getLong(CU_ARGLACCOUNTID);
272                if(rs.wasNull())
273                    cu_arglaccountid = null;
274                
275                Boolean cu_holduntilcomplete = rs.getInt(CU_HOLDUNTILCOMPLETE) == 1;
276                if(rs.wasNull())
277                    cu_holduntilcomplete = null;
278                
279                Boolean cu_allowbackorders = rs.getInt(CU_ALLOWBACKORDERS) == 1;
280                if(rs.wasNull())
281                    cu_allowbackorders = null;
282                
283                Boolean cu_allowsubstitutions = rs.getInt(CU_ALLOWSUBSTITUTIONS) == 1;
284                if(rs.wasNull())
285                    cu_allowsubstitutions = null;
286                
287                Boolean cu_allowcombiningshipments = rs.getInt(CU_ALLOWCOMBININGSHIPMENTS) == 1;
288                if(rs.wasNull())
289                    cu_allowcombiningshipments = null;
290                
291                Boolean cu_requirereference = rs.getInt(CU_REQUIREREFERENCE) == 1;
292                if(rs.wasNull())
293                    cu_requirereference = null;
294                
295                Boolean cu_allowreferenceduplicates = rs.getInt(CU_ALLOWREFERENCEDUPLICATES) == 1;
296                if(rs.wasNull())
297                    cu_allowreferenceduplicates = null;
298                
299                String cu_referencevalidationpattern = rs.getString(CU_REFERENCEVALIDATIONPATTERN);
300                if(rs.wasNull())
301                    cu_referencevalidationpattern = null;
302                
303                Long cu_fromtime = rs.getLong(CU_FROMTIME);
304                if(rs.wasNull())
305                    cu_fromtime = null;
306                
307                Long cu_thrutime = rs.getLong(CU_THRUTIME);
308                if(rs.wasNull())
309                    cu_thrutime = null;
310                
311                _value = new CustomerValue(_pk, new PartyPK(cu_par_partyid), cu_customername, new CustomerTypePK(cu_cuty_customertypeid), new OfferUsePK(cu_initialofferuseid), new CancellationPolicyPK(cu_cnclplcy_cancellationpolicyid), new ReturnPolicyPK(cu_rtnplcy_returnpolicyid), new GlAccountPK(cu_arglaccountid), cu_holduntilcomplete, cu_allowbackorders, cu_allowsubstitutions, cu_allowcombiningshipments, cu_requirereference, cu_allowreferenceduplicates, cu_referencevalidationpattern, cu_fromtime, cu_thrutime);
312            } else
313                _value = _entity.getCustomerValue();
314        } catch (SQLException se) {
315            throw new PersistenceDatabaseException(se);
316        }
317        
318        return _value;
319    }
320    
321    public java.util.List<Customer> getEntitiesFromPKs(EntityPermission entityPermission, Collection<CustomerPK> pks)
322            throws PersistenceDatabaseException {
323        return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks);
324    }
325    
326    public java.util.List<Customer> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<CustomerPK> pks)
327            throws PersistenceDatabaseException {
328        java.util.List<Customer> _entities = new ArrayList<>(pks.size());
329        
330        for(CustomerPK _pk: pks) {
331            _entities.add(getEntityFromPK(session, entityPermission, _pk));
332        }
333        
334        return _entities;
335    }
336    
337    public Customer getEntityFromValue(EntityPermission entityPermission, CustomerValue value) {
338        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey());
339    }
340    
341    public Customer getEntityFromValue(Session session, EntityPermission entityPermission, CustomerValue value) {
342        return getEntityFromPK(session, entityPermission, value.getPrimaryKey());
343    }
344    
345    public Customer getEntityFromPK(EntityPermission entityPermission, CustomerPK pk)
346            throws PersistenceDatabaseException {
347        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk);
348    }
349    
350    public Customer getEntityFromCache(Session session, CustomerPK pk) {
351        CustomerValue _value = (CustomerValue)session.getValueCache().get(pk);
352    
353        return _value == null ? null : new Customer(_value, EntityPermission.READ_ONLY);
354    }
355    
356    public Customer getEntityFromPK(Session session, EntityPermission entityPermission, CustomerPK pk)
357            throws PersistenceDatabaseException {
358        Customer _entity;
359        
360        // See if we already have the entity in the session cache
361        _entity = (Customer)session.getEntity(pk);
362        if(_entity != null) {
363            // If the requested permission is READ_WRITE, and the cached permission is
364            // READ_ONLY, then pretend that the cached object wasn't found, and create
365            // a new entity that is READ_WRITE.
366            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
367                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
368                    _entity = null;
369            }
370        }
371        
372        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
373            _entity = getEntityFromCache(session, pk);
374        }
375        
376        if(_entity == null) {
377            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
378            long _entityId = pk.getEntityId();
379            ResultSet _rs = null;
380            
381            try {
382                _ps.setLong(1, _entityId);
383                _rs = _ps.executeQuery();
384                if(_rs.next()) {
385                    _entity = getEntityFromResultSet(session, entityPermission, _rs);
386                }
387            } catch (SQLException se) {
388                throw new PersistenceDatabaseException(se);
389            } finally {
390                if(_rs != null) {
391                    try {
392                        _rs.close();
393                    } catch (SQLException se) {
394                        // do nothing
395                    }
396                }
397            }
398        }
399        
400        return _entity;
401    }
402    
403    public Set<CustomerPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
404            throws PersistenceDatabaseException {
405        Set<CustomerPK> _pks;
406        ResultSet _rs = null;
407        
408        try {
409            if(params.length != 0) {
410                Session.setQueryParams(ps, params);
411            }
412            
413            _rs = ps.executeQuery();
414            _pks = getPKsFromResultSetAsSet(_rs);
415            _rs.close();
416        } catch (SQLException se) {
417            throw new PersistenceDatabaseException(se);
418        } finally {
419            if(_rs != null) {
420                try {
421                    _rs.close();
422                } catch (SQLException se) {
423                    // do nothing
424                }
425            }
426        }
427        
428        return _pks;
429    }
430    
431    public java.util.List<CustomerPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
432            throws PersistenceDatabaseException {
433        java.util.List<CustomerPK> _pks;
434        ResultSet _rs = null;
435        
436        try {
437            if(params.length != 0) {
438                Session.setQueryParams(ps, params);
439            }
440            
441            _rs = ps.executeQuery();
442            _pks = getPKsFromResultSetAsList(_rs);
443            _rs.close();
444        } catch (SQLException se) {
445            throw new PersistenceDatabaseException(se);
446        } finally {
447            if(_rs != null) {
448                try {
449                    _rs.close();
450                } catch (SQLException se) {
451                    // do nothing
452                }
453            }
454        }
455        
456        return _pks;
457    }
458    
459    public CustomerPK getPKFromQuery(PreparedStatement ps, final Object... params)
460            throws PersistenceDatabaseException {
461        CustomerPK _pk = null;
462        ResultSet _rs = null;
463        
464        try {
465            if(params.length != 0) {
466                Session.setQueryParams(ps, params);
467            }
468            
469            _rs = ps.executeQuery();
470            if(_rs.next()) {
471                _pk = getPKFromResultSet(_rs);
472            }
473            _rs.close();
474        } catch (SQLException se) {
475            throw new PersistenceDatabaseException(se);
476        } finally {
477            if(_rs != null) {
478                try {
479                    _rs.close();
480                } catch (SQLException se) {
481                    // do nothing
482                }
483            }
484        }
485        
486        return _pk;
487    }
488    
489    public java.util.List<Customer> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
490            throws PersistenceDatabaseException {
491        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
492        
493        return getEntitiesFromQuery(session, entityPermission, ps, params);
494    }
495    
496    public java.util.List<Customer> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
497            throws PersistenceDatabaseException {
498        Session session = ThreadSession.currentSession();
499        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
500        
501        return getEntitiesFromQuery(session, entityPermission, ps, params);
502    }
503    
504    public java.util.List<Customer> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
505            throws PersistenceDatabaseException {
506        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
507        
508        return getEntitiesFromQuery(session, entityPermission, ps);
509    }
510    
511    public java.util.List<Customer> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
512            throws PersistenceDatabaseException {
513        Session session = ThreadSession.currentSession();
514        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
515        
516        return getEntitiesFromQuery(session, entityPermission, ps);
517    }
518    
519    public java.util.List<Customer> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps)
520            throws PersistenceDatabaseException {
521        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps);
522    }
523    
524    public java.util.List<Customer> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
525            throws PersistenceDatabaseException {
526        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
527    }
528    
529    public java.util.List<Customer> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
530            throws PersistenceDatabaseException {
531        java.util.List<Customer> _entities;
532        ResultSet _rs = null;
533        
534        try {
535            if(params.length != 0) {
536                Session.setQueryParams(ps, params);
537            }
538            
539            _rs = ps.executeQuery();
540            _entities = getEntitiesFromResultSet(session, entityPermission, _rs);
541            _rs.close();
542        } catch (SQLException se) {
543            throw new PersistenceDatabaseException(se);
544        } finally {
545            if(_rs != null) {
546                try {
547                    _rs.close();
548                } catch (SQLException se) {
549                    // do nothing
550                }
551            }
552        }
553        
554        return _entities;
555    }
556    
557    public Customer getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
558            throws PersistenceDatabaseException {
559        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
560        
561        return getEntityFromQuery(session, entityPermission, ps, params);
562    }
563    
564    public Customer getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
565            throws PersistenceDatabaseException {
566        Session session = ThreadSession.currentSession();
567        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
568        
569        return getEntityFromQuery(session, entityPermission, ps, params);
570    }
571    
572    public Customer getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
573            throws PersistenceDatabaseException {
574        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
575        
576        return getEntityFromQuery(session, entityPermission, ps);
577    }
578    
579    public Customer getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
580            throws PersistenceDatabaseException {
581        Session session = ThreadSession.currentSession();
582        PreparedStatement ps = session.prepareStatement(CustomerFactory.class, queryMap.get(entityPermission));
583        
584        return getEntityFromQuery(session, entityPermission, ps);
585    }
586    
587    public Customer getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps)
588            throws PersistenceDatabaseException {
589        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps);
590    }
591    
592    public Customer getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
593            throws PersistenceDatabaseException {
594        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
595    }
596    
597    public Customer getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
598            throws PersistenceDatabaseException {
599        Customer _entity = null;
600        ResultSet _rs = null;
601        
602        try {
603            if(params.length != 0) {
604                Session.setQueryParams(ps, params);
605            }
606            
607            _rs = ps.executeQuery();
608            if(_rs.next()) {
609                _entity = getEntityFromResultSet(session, entityPermission, _rs);
610            }
611            _rs.close();
612        } catch (SQLException se) {
613            throw new PersistenceDatabaseException(se);
614        } finally {
615            if(_rs != null) {
616                try {
617                    _rs.close();
618                } catch (SQLException se) {
619                    // do nothing
620                }
621            }
622        }
623        
624        return _entity;
625    }
626    
627    public java.util.List<Customer> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
628            throws PersistenceDatabaseException {
629        return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
630    }
631    
632    public java.util.List<Customer> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
633            throws PersistenceDatabaseException {
634        java.util.List<Customer> _result = new ArrayList<>();
635        
636        try {
637            while(rs.next()) {
638                _result.add(getEntityFromResultSet(session, entityPermission, rs));
639            }
640        } catch (SQLException se) {
641            throw new PersistenceDatabaseException(se);
642        }
643        
644        return _result;
645    }
646    
647    public Customer getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
648            throws PersistenceDatabaseException {
649        return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
650    }
651    
652    public Customer getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
653            throws PersistenceDatabaseException {
654        Customer _entity;
655        
656        try {
657            Long cu_customerid = rs.getLong(CU_CUSTOMERID);
658            CustomerPK _pk = new CustomerPK(cu_customerid);
659            
660            // See if we already have the entity in the session cache
661            _entity = (Customer)session.getEntity(_pk);
662            if(_entity != null) {
663                // If the requested permission is READ_WRITE, and the cached permission is
664                // READ_ONLY, then pretend that the cached object wasn't found, and create
665                // a new entity that is READ_WRITE.
666                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
667                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
668                        _entity = null;
669                }
670            }
671            boolean foundInSessionCache = _entity != null;
672            
673            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
674                _entity = getEntityFromCache(session, _pk);
675            }
676            
677            if(_entity == null) {
678                Long cu_par_partyid = rs.getLong(CU_PAR_PARTYID);
679                if(rs.wasNull())
680                    cu_par_partyid = null;
681                
682                String cu_customername = rs.getString(CU_CUSTOMERNAME);
683                if(rs.wasNull())
684                    cu_customername = null;
685                
686                Long cu_cuty_customertypeid = rs.getLong(CU_CUTY_CUSTOMERTYPEID);
687                if(rs.wasNull())
688                    cu_cuty_customertypeid = null;
689                
690                Long cu_initialofferuseid = rs.getLong(CU_INITIALOFFERUSEID);
691                if(rs.wasNull())
692                    cu_initialofferuseid = null;
693                
694                Long cu_cnclplcy_cancellationpolicyid = rs.getLong(CU_CNCLPLCY_CANCELLATIONPOLICYID);
695                if(rs.wasNull())
696                    cu_cnclplcy_cancellationpolicyid = null;
697                
698                Long cu_rtnplcy_returnpolicyid = rs.getLong(CU_RTNPLCY_RETURNPOLICYID);
699                if(rs.wasNull())
700                    cu_rtnplcy_returnpolicyid = null;
701                
702                Long cu_arglaccountid = rs.getLong(CU_ARGLACCOUNTID);
703                if(rs.wasNull())
704                    cu_arglaccountid = null;
705                
706                Boolean cu_holduntilcomplete = rs.getInt(CU_HOLDUNTILCOMPLETE) == 1;
707                if(rs.wasNull())
708                    cu_holduntilcomplete = null;
709                
710                Boolean cu_allowbackorders = rs.getInt(CU_ALLOWBACKORDERS) == 1;
711                if(rs.wasNull())
712                    cu_allowbackorders = null;
713                
714                Boolean cu_allowsubstitutions = rs.getInt(CU_ALLOWSUBSTITUTIONS) == 1;
715                if(rs.wasNull())
716                    cu_allowsubstitutions = null;
717                
718                Boolean cu_allowcombiningshipments = rs.getInt(CU_ALLOWCOMBININGSHIPMENTS) == 1;
719                if(rs.wasNull())
720                    cu_allowcombiningshipments = null;
721                
722                Boolean cu_requirereference = rs.getInt(CU_REQUIREREFERENCE) == 1;
723                if(rs.wasNull())
724                    cu_requirereference = null;
725                
726                Boolean cu_allowreferenceduplicates = rs.getInt(CU_ALLOWREFERENCEDUPLICATES) == 1;
727                if(rs.wasNull())
728                    cu_allowreferenceduplicates = null;
729                
730                String cu_referencevalidationpattern = rs.getString(CU_REFERENCEVALIDATIONPATTERN);
731                if(rs.wasNull())
732                    cu_referencevalidationpattern = null;
733                
734                Long cu_fromtime = rs.getLong(CU_FROMTIME);
735                if(rs.wasNull())
736                    cu_fromtime = null;
737                
738                Long cu_thrutime = rs.getLong(CU_THRUTIME);
739                if(rs.wasNull())
740                    cu_thrutime = null;
741                
742                CustomerValue _value = new CustomerValue(_pk, cu_par_partyid == null? null: new PartyPK(cu_par_partyid), cu_customername, cu_cuty_customertypeid == null? null: new CustomerTypePK(cu_cuty_customertypeid), cu_initialofferuseid == null? null: new OfferUsePK(cu_initialofferuseid), cu_cnclplcy_cancellationpolicyid == null? null: new CancellationPolicyPK(cu_cnclplcy_cancellationpolicyid), cu_rtnplcy_returnpolicyid == null? null: new ReturnPolicyPK(cu_rtnplcy_returnpolicyid), cu_arglaccountid == null? null: new GlAccountPK(cu_arglaccountid), cu_holduntilcomplete, cu_allowbackorders, cu_allowsubstitutions, cu_allowcombiningshipments, cu_requirereference, cu_allowreferenceduplicates, cu_referencevalidationpattern, cu_fromtime, cu_thrutime);
743                _entity = new Customer(_value, entityPermission);
744            }
745            
746            if(!foundInSessionCache) {
747                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
748                    session.putReadOnlyEntity(_pk, _entity);
749                    session.getValueCache().put(_entity.getCustomerValue());
750                } else {
751                    session.putReadWriteEntity(_pk, _entity);
752                }
753            }
754        } catch (SQLException se) {
755            throw new PersistenceDatabaseException(se);
756        }
757        
758        return _entity;
759    }
760    
761    public Customer create(Session session, Party party, String customerName, CustomerType customerType, OfferUse initialOfferUse, CancellationPolicy cancellationPolicy, ReturnPolicy returnPolicy, GlAccount arGlAccount, Boolean holdUntilComplete, Boolean allowBackorders, Boolean allowSubstitutions, Boolean allowCombiningShipments, Boolean requireReference, Boolean allowReferenceDuplicates, String referenceValidationPattern, Long fromTime, Long thruTime)
762            throws PersistenceDatabaseException, PersistenceNotNullException {
763        return create(session, party == null ? null : party.getPrimaryKey(), customerName, customerType == null ? null : customerType.getPrimaryKey(), initialOfferUse == null ? null : initialOfferUse.getPrimaryKey(), cancellationPolicy == null ? null : cancellationPolicy.getPrimaryKey(), returnPolicy == null ? null : returnPolicy.getPrimaryKey(), arGlAccount == null ? null : arGlAccount.getPrimaryKey(), holdUntilComplete, allowBackorders, allowSubstitutions, allowCombiningShipments, requireReference, allowReferenceDuplicates, referenceValidationPattern, fromTime, thruTime);
764    }
765    
766    public Customer create(Party party, String customerName, CustomerType customerType, OfferUse initialOfferUse, CancellationPolicy cancellationPolicy, ReturnPolicy returnPolicy, GlAccount arGlAccount, Boolean holdUntilComplete, Boolean allowBackorders, Boolean allowSubstitutions, Boolean allowCombiningShipments, Boolean requireReference, Boolean allowReferenceDuplicates, String referenceValidationPattern, Long fromTime, Long thruTime)
767            throws PersistenceDatabaseException, PersistenceNotNullException {
768        return create(ThreadSession.currentSession(), party == null ? null : party.getPrimaryKey(), customerName, customerType == null ? null : customerType.getPrimaryKey(), initialOfferUse == null ? null : initialOfferUse.getPrimaryKey(), cancellationPolicy == null ? null : cancellationPolicy.getPrimaryKey(), returnPolicy == null ? null : returnPolicy.getPrimaryKey(), arGlAccount == null ? null : arGlAccount.getPrimaryKey(), holdUntilComplete, allowBackorders, allowSubstitutions, allowCombiningShipments, requireReference, allowReferenceDuplicates, referenceValidationPattern, fromTime, thruTime);
769    }
770    
771    private void bindForCreate(PreparedStatement _ps, CustomerValue _value)
772            throws SQLException {
773        _ps.setLong(1, _value.getEntityId());
774        
775        PartyPK cu_par_partyid = _value.getPartyPK();
776        if(cu_par_partyid == null)
777            _ps.setNull(2, Types.BIGINT);
778        else
779            _ps.setLong(2, cu_par_partyid.getEntityId());
780            
781        String cu_customername = _value.getCustomerName();
782        if(cu_customername == null)
783            _ps.setNull(3, Types.VARCHAR);
784        else
785            _ps.setString(3, cu_customername);
786            
787        CustomerTypePK cu_cuty_customertypeid = _value.getCustomerTypePK();
788        if(cu_cuty_customertypeid == null)
789            _ps.setNull(4, Types.BIGINT);
790        else
791            _ps.setLong(4, cu_cuty_customertypeid.getEntityId());
792            
793        OfferUsePK cu_initialofferuseid = _value.getInitialOfferUsePK();
794        if(cu_initialofferuseid == null)
795            _ps.setNull(5, Types.BIGINT);
796        else
797            _ps.setLong(5, cu_initialofferuseid.getEntityId());
798            
799        CancellationPolicyPK cu_cnclplcy_cancellationpolicyid = _value.getCancellationPolicyPK();
800        if(cu_cnclplcy_cancellationpolicyid == null)
801            _ps.setNull(6, Types.BIGINT);
802        else
803            _ps.setLong(6, cu_cnclplcy_cancellationpolicyid.getEntityId());
804            
805        ReturnPolicyPK cu_rtnplcy_returnpolicyid = _value.getReturnPolicyPK();
806        if(cu_rtnplcy_returnpolicyid == null)
807            _ps.setNull(7, Types.BIGINT);
808        else
809            _ps.setLong(7, cu_rtnplcy_returnpolicyid.getEntityId());
810            
811        GlAccountPK cu_arglaccountid = _value.getArGlAccountPK();
812        if(cu_arglaccountid == null)
813            _ps.setNull(8, Types.BIGINT);
814        else
815            _ps.setLong(8, cu_arglaccountid.getEntityId());
816            
817        Boolean cu_holduntilcomplete = _value.getHoldUntilComplete();
818        if(cu_holduntilcomplete == null)
819            _ps.setNull(9, Types.BIT);
820        else
821            _ps.setInt(9, cu_holduntilcomplete? 1: 0);
822            
823        Boolean cu_allowbackorders = _value.getAllowBackorders();
824        if(cu_allowbackorders == null)
825            _ps.setNull(10, Types.BIT);
826        else
827            _ps.setInt(10, cu_allowbackorders? 1: 0);
828            
829        Boolean cu_allowsubstitutions = _value.getAllowSubstitutions();
830        if(cu_allowsubstitutions == null)
831            _ps.setNull(11, Types.BIT);
832        else
833            _ps.setInt(11, cu_allowsubstitutions? 1: 0);
834            
835        Boolean cu_allowcombiningshipments = _value.getAllowCombiningShipments();
836        if(cu_allowcombiningshipments == null)
837            _ps.setNull(12, Types.BIT);
838        else
839            _ps.setInt(12, cu_allowcombiningshipments? 1: 0);
840            
841        Boolean cu_requirereference = _value.getRequireReference();
842        if(cu_requirereference == null)
843            _ps.setNull(13, Types.BIT);
844        else
845            _ps.setInt(13, cu_requirereference? 1: 0);
846            
847        Boolean cu_allowreferenceduplicates = _value.getAllowReferenceDuplicates();
848        if(cu_allowreferenceduplicates == null)
849            _ps.setNull(14, Types.BIT);
850        else
851            _ps.setInt(14, cu_allowreferenceduplicates? 1: 0);
852            
853        String cu_referencevalidationpattern = _value.getReferenceValidationPattern();
854        if(cu_referencevalidationpattern == null)
855            _ps.setNull(15, Types.VARCHAR);
856        else
857            _ps.setString(15, cu_referencevalidationpattern);
858            
859        Long cu_fromtime = _value.getFromTime();
860        if(cu_fromtime == null)
861            _ps.setNull(16, Types.BIGINT);
862        else
863            _ps.setLong(16, cu_fromtime);
864            
865        Long cu_thrutime = _value.getThruTime();
866        if(cu_thrutime == null)
867            _ps.setNull(17, Types.BIGINT);
868        else
869            _ps.setLong(17, cu_thrutime);
870            
871    }
872    
873    public Customer create(Session session, PartyPK partyPK, String customerName, CustomerTypePK customerTypePK, OfferUsePK initialOfferUsePK, CancellationPolicyPK cancellationPolicyPK, ReturnPolicyPK returnPolicyPK, GlAccountPK arGlAccountPK, Boolean holdUntilComplete, Boolean allowBackorders, Boolean allowSubstitutions, Boolean allowCombiningShipments, Boolean requireReference, Boolean allowReferenceDuplicates, String referenceValidationPattern, Long fromTime, Long thruTime)
874            throws PersistenceDatabaseException, PersistenceNotNullException {
875        CustomerPK _pk = getNextPK();
876        CustomerValue _value = new CustomerValue(_pk, partyPK, customerName, customerTypePK, initialOfferUsePK, cancellationPolicyPK, returnPolicyPK, arGlAccountPK, holdUntilComplete, allowBackorders, allowSubstitutions, allowCombiningShipments, requireReference, allowReferenceDuplicates, referenceValidationPattern, fromTime, thruTime);
877        
878        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
879        
880        try {
881            bindForCreate(_ps, _value);
882            
883            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
884                int _count = _ps.executeUpdate();
885                
886                if(_count != 1) {
887                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
888                }
889            } else {
890                 _ps.executeUpdate();
891            }
892            
893            session.getValueCache().put(_value);
894        } catch (SQLException se) {
895            throw new PersistenceDatabaseException(se);
896        }
897        
898        Customer _entity = new Customer(_value, EntityPermission.READ_ONLY);
899        session.putReadOnlyEntity(_pk, _entity);
900        
901        return _entity;
902    }
903    
904    public Customer create(PartyPK partyPK, String customerName, CustomerTypePK customerTypePK, OfferUsePK initialOfferUsePK, CancellationPolicyPK cancellationPolicyPK, ReturnPolicyPK returnPolicyPK, GlAccountPK arGlAccountPK, Boolean holdUntilComplete, Boolean allowBackorders, Boolean allowSubstitutions, Boolean allowCombiningShipments, Boolean requireReference, Boolean allowReferenceDuplicates, String referenceValidationPattern, Long fromTime, Long thruTime)
905            throws PersistenceDatabaseException, PersistenceNotNullException {
906        return create(ThreadSession.currentSession(), partyPK, customerName, customerTypePK, initialOfferUsePK, cancellationPolicyPK, returnPolicyPK, arGlAccountPK, holdUntilComplete, allowBackorders, allowSubstitutions, allowCombiningShipments, requireReference, allowReferenceDuplicates, referenceValidationPattern, fromTime, thruTime);
907    }
908    
909    public void create(Session session, Collection<CustomerValue> _values)
910            throws PersistenceDatabaseException, PersistenceNotNullException {
911        int _size = _values.size();
912        
913        if(_size > 0) {
914            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
915            List<CustomerValue> _cacheValues = new ArrayList<>(_size);
916            
917            try {
918                for(CustomerValue _value : _values) {
919                    _value.setEntityId(entityIdGenerator.getNextEntityId());
920                    bindForCreate(_ps, _value);
921                    
922                    _ps.addBatch();
923                    
924                    _cacheValues.add(_value);
925                }
926                
927                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
928                    int[] _counts = _ps.executeBatch();
929                    
930                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
931                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
932                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
933                        }
934                    }
935                } else {
936                     _ps.executeBatch();
937                }
938                
939                _ps.clearBatch();
940            } catch (SQLException se) {
941                throw new PersistenceDatabaseException(se);
942            }
943            
944            _cacheValues.forEach((_cacheValue) -> {
945                Customer _cacheEntity = new Customer(_cacheValue, EntityPermission.READ_ONLY);
946                
947                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
948            });
949        }
950    }
951    
952    public void create(Collection<CustomerValue> _values)
953            throws PersistenceDatabaseException, PersistenceNotNullException {
954        create(ThreadSession.currentSession(), _values);
955    }
956    
957    private boolean bindForStore(PreparedStatement _ps, CustomerValue _value)
958            throws SQLException {
959        boolean _hasBeenModified = _value.hasBeenModified();
960        
961        if(_hasBeenModified) {
962            PartyPK cu_par_partyid = _value.getPartyPK();
963            if(cu_par_partyid == null)
964                _ps.setNull(1, Types.BIGINT);
965            else
966                _ps.setLong(1, cu_par_partyid.getEntityId());
967            
968            String cu_customername = _value.getCustomerName();
969            if(cu_customername == null)
970                _ps.setNull(2, Types.VARCHAR);
971            else
972                _ps.setString(2, cu_customername);
973            
974            CustomerTypePK cu_cuty_customertypeid = _value.getCustomerTypePK();
975            if(cu_cuty_customertypeid == null)
976                _ps.setNull(3, Types.BIGINT);
977            else
978                _ps.setLong(3, cu_cuty_customertypeid.getEntityId());
979            
980            OfferUsePK cu_initialofferuseid = _value.getInitialOfferUsePK();
981            if(cu_initialofferuseid == null)
982                _ps.setNull(4, Types.BIGINT);
983            else
984                _ps.setLong(4, cu_initialofferuseid.getEntityId());
985            
986            CancellationPolicyPK cu_cnclplcy_cancellationpolicyid = _value.getCancellationPolicyPK();
987            if(cu_cnclplcy_cancellationpolicyid == null)
988                _ps.setNull(5, Types.BIGINT);
989            else
990                _ps.setLong(5, cu_cnclplcy_cancellationpolicyid.getEntityId());
991            
992            ReturnPolicyPK cu_rtnplcy_returnpolicyid = _value.getReturnPolicyPK();
993            if(cu_rtnplcy_returnpolicyid == null)
994                _ps.setNull(6, Types.BIGINT);
995            else
996                _ps.setLong(6, cu_rtnplcy_returnpolicyid.getEntityId());
997            
998            GlAccountPK cu_arglaccountid = _value.getArGlAccountPK();
999            if(cu_arglaccountid == null)
1000                _ps.setNull(7, Types.BIGINT);
1001            else
1002                _ps.setLong(7, cu_arglaccountid.getEntityId());
1003            
1004            Boolean cu_holduntilcomplete = _value.getHoldUntilComplete();
1005            if(cu_holduntilcomplete == null)
1006                _ps.setNull(8, Types.BIT);
1007            else
1008                _ps.setInt(8, cu_holduntilcomplete? 1: 0);
1009            
1010            Boolean cu_allowbackorders = _value.getAllowBackorders();
1011            if(cu_allowbackorders == null)
1012                _ps.setNull(9, Types.BIT);
1013            else
1014                _ps.setInt(9, cu_allowbackorders? 1: 0);
1015            
1016            Boolean cu_allowsubstitutions = _value.getAllowSubstitutions();
1017            if(cu_allowsubstitutions == null)
1018                _ps.setNull(10, Types.BIT);
1019            else
1020                _ps.setInt(10, cu_allowsubstitutions? 1: 0);
1021            
1022            Boolean cu_allowcombiningshipments = _value.getAllowCombiningShipments();
1023            if(cu_allowcombiningshipments == null)
1024                _ps.setNull(11, Types.BIT);
1025            else
1026                _ps.setInt(11, cu_allowcombiningshipments? 1: 0);
1027            
1028            Boolean cu_requirereference = _value.getRequireReference();
1029            if(cu_requirereference == null)
1030                _ps.setNull(12, Types.BIT);
1031            else
1032                _ps.setInt(12, cu_requirereference? 1: 0);
1033            
1034            Boolean cu_allowreferenceduplicates = _value.getAllowReferenceDuplicates();
1035            if(cu_allowreferenceduplicates == null)
1036                _ps.setNull(13, Types.BIT);
1037            else
1038                _ps.setInt(13, cu_allowreferenceduplicates? 1: 0);
1039            
1040            String cu_referencevalidationpattern = _value.getReferenceValidationPattern();
1041            if(cu_referencevalidationpattern == null)
1042                _ps.setNull(14, Types.VARCHAR);
1043            else
1044                _ps.setString(14, cu_referencevalidationpattern);
1045            
1046            Long cu_fromtime = _value.getFromTime();
1047            if(cu_fromtime == null)
1048                _ps.setNull(15, Types.BIGINT);
1049            else
1050                _ps.setLong(15, cu_fromtime);
1051            
1052            Long cu_thrutime = _value.getThruTime();
1053            if(cu_thrutime == null)
1054                _ps.setNull(16, Types.BIGINT);
1055            else
1056                _ps.setLong(16, cu_thrutime);
1057            
1058            _ps.setLong(17, _value.getPrimaryKey().getEntityId());
1059            
1060            _value.clearHasBeenModified();
1061        }
1062        
1063        return _hasBeenModified;
1064    }
1065    
1066    @Override
1067    public void store(Session session, Customer entity)
1068            throws PersistenceDatabaseException {
1069        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
1070        
1071        try {
1072            CustomerValue _value = entity.getCustomerValue();
1073            
1074            if(bindForStore(_ps, _value)) {
1075                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
1076                    int _count = _ps.executeUpdate();
1077                    
1078                    if(_count != 1) {
1079                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
1080                    }
1081                } else {
1082                     _ps.executeUpdate();
1083                }
1084                
1085                session.getValueCache().put(_value);
1086            }
1087        } catch (SQLException se) {
1088            throw new PersistenceDatabaseException(se);
1089        }
1090    }
1091    
1092    @Override
1093    public void store(Session session, Collection<Customer> entities)
1094            throws PersistenceDatabaseException {
1095        if(entities.size() > 0) {
1096            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
1097            int _modifiedEntities = 0;
1098            
1099            try {
1100                for(Customer entity : entities) {
1101                    if(bindForStore(_ps, entity.getCustomerValue())) {
1102                        _ps.addBatch();
1103                        _modifiedEntities++;
1104                    }
1105                }
1106                
1107                if(_modifiedEntities != 0) {
1108                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
1109                        int[] _counts = _ps.executeBatch();
1110                        
1111                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
1112                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
1113                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
1114                            }
1115                        }
1116                    } else {
1117                         _ps.executeBatch();
1118                    }
1119                    
1120                    _ps.clearBatch();
1121                    
1122                    entities.forEach((entity) -> {
1123                        session.getValueCache().put(entity.getCustomerValue());
1124                    });
1125                }
1126            } catch (SQLException se) {
1127                throw new PersistenceDatabaseException(se);
1128            }
1129        }
1130    }
1131    
1132    @Override
1133    public void store(Collection<Customer> entities)
1134            throws PersistenceDatabaseException {
1135        store(ThreadSession.currentSession(), entities);
1136    }
1137    
1138    @Override
1139    public void remove(Session session, Customer entity)
1140            throws PersistenceDatabaseException {
1141        remove(session, entity.getPrimaryKey());
1142    }
1143    
1144    @Override
1145    public void remove(Session session, CustomerPK pk)
1146            throws PersistenceDatabaseException {
1147        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
1148        long _entityId = pk.getEntityId();
1149        
1150        try {
1151            _ps.setLong(1, _entityId);
1152            
1153            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
1154                int _count = _ps.executeUpdate();
1155                
1156                if(_count != 1) {
1157                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
1158                }
1159            } else {
1160                 _ps.executeUpdate();
1161            }
1162            
1163            session.getValueCache().remove(pk);
1164        } catch (SQLException se) {
1165            throw new PersistenceDatabaseException(se);
1166        }
1167        
1168        session.removed(pk, false);
1169    }
1170    
1171    @Override
1172    public void remove(Session session, Collection<CustomerPK> pks)
1173            throws PersistenceDatabaseException {
1174        if(pks.size() > 0) {
1175            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
1176            int _modifiedEntities = 0;
1177            
1178            try {
1179                for(CustomerPK pk : pks) {
1180                    long _entityId = pk.getEntityId();
1181                    
1182                    _ps.setLong(1, _entityId);
1183                    
1184                    _ps.addBatch();
1185                    _modifiedEntities++;
1186                }
1187                
1188                if(_modifiedEntities != 0) {
1189                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
1190                        int[] _counts = _ps.executeBatch();
1191                        
1192                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
1193                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
1194                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
1195                            }
1196                        }
1197                    } else {
1198                        _ps.executeBatch();
1199                    }
1200                    
1201                    _ps.clearBatch();
1202                    
1203                    pks.forEach((pk) -> {
1204                        session.getValueCache().remove(pk);
1205                    });
1206                }
1207            } catch (SQLException se) {
1208                throw new PersistenceDatabaseException(se);
1209            }
1210            
1211            pks.forEach((pk) -> {
1212                session.removed(pk, true);
1213            });
1214        }
1215    }
1216    
1217    @Override
1218    public void remove(Collection<CustomerPK> pks)
1219            throws PersistenceDatabaseException {
1220        remove(ThreadSession.currentSession(), pks);
1221    }
1222    
1223    @Override
1224    public boolean validPK(Session session, CustomerPK pk)
1225            throws PersistenceDatabaseException {
1226        boolean valid = false;
1227        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
1228        ResultSet _rs = null;
1229        
1230        try {
1231            _ps.setLong(1, pk.getEntityId());
1232            
1233            _rs = _ps.executeQuery();
1234            if(_rs.next()) {
1235                long _count = _rs.getLong(1);
1236                if(_rs.wasNull())
1237                    _count = 0;
1238                
1239                if(_count == 1)
1240                    valid = true;
1241            }
1242        } catch (SQLException se) {
1243            throw new PersistenceDatabaseException(se);
1244        } finally {
1245            if(_rs != null) {
1246                try {
1247                    _rs.close();
1248                } catch (SQLException se) {
1249                    // do nothing
1250                }
1251            }
1252        }
1253        
1254        return valid;
1255    }
1256    
1257}