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