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