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