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