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