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