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 * ShipmentPackageStatusFactory.java
021 */
022
023package com.echothree.model.data.shipment.server.factory;
024
025import com.echothree.model.data.shipment.common.pk.ShipmentPackagePK;
026
027import com.echothree.model.data.shipment.server.entity.ShipmentPackage;
028
029import com.echothree.model.data.shipment.common.ShipmentPackageStatusConstants;
030import com.echothree.model.data.shipment.common.pk.ShipmentPackageStatusPK;
031import com.echothree.model.data.shipment.server.value.ShipmentPackageStatusValue;
032import com.echothree.model.data.shipment.server.entity.ShipmentPackageStatus;
033import com.echothree.util.common.exception.PersistenceDatabaseException;
034import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
035import com.echothree.util.common.exception.PersistenceNotNullException;
036import com.echothree.util.server.persistence.BaseFactory;
037import com.echothree.util.server.persistence.EntityIdGenerator;
038import com.echothree.util.server.persistence.EntityPermission;
039import com.echothree.util.server.persistence.PersistenceDebugFlags;
040import com.echothree.util.server.persistence.Session;
041import java.sql.PreparedStatement;
042import java.sql.ResultSet;
043import java.sql.SQLException;
044import java.sql.Types;
045import java.io.ByteArrayInputStream;
046import java.io.StringReader;
047import java.util.ArrayList;
048import java.util.Collection;
049import java.util.HashSet;
050import java.util.List;
051import java.util.Map;
052import java.util.Set;
053import javax.enterprise.context.ApplicationScoped;
054import javax.enterprise.inject.spi.CDI;
055import javax.inject.Inject;
056import org.jooq.ResultQuery;
057import org.slf4j.Logger;
058import org.slf4j.LoggerFactory;
059
060@ApplicationScoped
061public class ShipmentPackageStatusFactory
062        implements BaseFactory<ShipmentPackageStatusPK, ShipmentPackageStatus> {
063    
064    @Inject
065    Session session;
066    
067    //private static final Logger log = LoggerFactory.getLogger(ShipmentPackageStatusFactory.class);
068    
069    final private static String SQL_SELECT_READ_ONLY = "SELECT shppckgst_shipmentpackagestatusid, shppckgst_shppckg_shipmentpackageid, shppckgst_shipmentpackagelinesequence FROM shipmentpackagestatuses WHERE shppckgst_shipmentpackagestatusid = ?";
070    final private static String SQL_SELECT_READ_WRITE = "SELECT shppckgst_shipmentpackagestatusid, shppckgst_shppckg_shipmentpackageid, shppckgst_shipmentpackagelinesequence FROM shipmentpackagestatuses WHERE shppckgst_shipmentpackagestatusid = ? FOR UPDATE";
071    final private static String SQL_INSERT = "INSERT INTO shipmentpackagestatuses (shppckgst_shipmentpackagestatusid, shppckgst_shppckg_shipmentpackageid, shppckgst_shipmentpackagelinesequence) VALUES (?, ?, ?)";
072    final private static String SQL_UPDATE = "UPDATE shipmentpackagestatuses SET shppckgst_shppckg_shipmentpackageid = ?, shppckgst_shipmentpackagelinesequence = ? WHERE shppckgst_shipmentpackagestatusid = ?";
073    final private static String SQL_DELETE = "DELETE FROM shipmentpackagestatuses WHERE shppckgst_shipmentpackagestatusid = ?";
074    final private static String SQL_VALID = "SELECT COUNT(*) FROM shipmentpackagestatuses WHERE shppckgst_shipmentpackagestatusid = ?";
075    
076    final public static String SHPPCKGST_SHIPMENTPACKAGESTATUSID = "shppckgst_shipmentpackagestatusid";
077    final public static String SHPPCKGST_SHPPCKG_SHIPMENTPACKAGEID = "shppckgst_shppckg_shipmentpackageid";
078    final public static String SHPPCKGST_SHIPMENTPACKAGELINESEQUENCE = "shppckgst_shipmentpackagelinesequence";
079    
080    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(ShipmentPackageStatusConstants.COMPONENT_VENDOR_NAME, ShipmentPackageStatusConstants.ENTITY_TYPE_NAME);
081    
082    /** Creates a new instance of ShipmentPackageStatusFactory */
083    protected ShipmentPackageStatusFactory() {
084        super();
085    }
086    
087    public static ShipmentPackageStatusFactory getInstance() {
088        return CDI.current().select(ShipmentPackageStatusFactory.class).get();
089    }
090    
091    @Override
092    public String getComponentVendorName() {
093        return ShipmentPackageStatusConstants.COMPONENT_VENDOR_NAME;
094    }
095    
096    @Override
097    public String getEntityTypeName() {
098        return ShipmentPackageStatusConstants.ENTITY_TYPE_NAME;
099    }
100    
101    public PreparedStatement prepareStatement(String query) {
102        return session.prepareStatement(query);
103    }
104    
105    public ShipmentPackageStatusPK getNextPK() {
106        return new ShipmentPackageStatusPK(entityIdGenerator.getNextEntityId());
107    }
108    
109    public Set<ShipmentPackageStatusPK> getPKsFromResultSetAsSet(ResultSet rs)
110            throws PersistenceDatabaseException {
111        Set<ShipmentPackageStatusPK> _result = new HashSet<>();
112        
113        try {
114            while(rs.next()) {
115                _result.add(getPKFromResultSet(rs));
116            }
117        } catch (SQLException se) {
118            throw new PersistenceDatabaseException(se);
119        }
120        
121        return _result;
122    }
123    
124    public java.util.List<ShipmentPackageStatusPK> getPKsFromResultSetAsList(ResultSet rs)
125            throws PersistenceDatabaseException {
126        java.util.List<ShipmentPackageStatusPK> _result = new ArrayList<>();
127        
128        try {
129            while(rs.next()) {
130                _result.add(getPKFromResultSet(rs));
131            }
132        } catch (SQLException se) {
133            throw new PersistenceDatabaseException(se);
134        }
135        
136        return _result;
137    }
138    
139    public ShipmentPackageStatusPK getPKFromResultSet(ResultSet rs)
140            throws PersistenceDatabaseException {
141        ShipmentPackageStatusPK _result;
142        
143        try {
144            long shppckgst_shipmentpackagestatusid = rs.getLong(SHPPCKGST_SHIPMENTPACKAGESTATUSID);
145            Long _entityId = rs.wasNull() ? null : shppckgst_shipmentpackagestatusid;
146            
147            _result = new ShipmentPackageStatusPK(_entityId);
148        } catch (SQLException se) {
149            throw new PersistenceDatabaseException(se);
150        }
151        
152        return _result;
153    }
154    
155    public java.util.List<ShipmentPackageStatusValue> getValuesFromPKs(Collection<ShipmentPackageStatusPK> pks)
156            throws PersistenceDatabaseException {
157        java.util.List<ShipmentPackageStatusValue> _values = new ArrayList<>(pks.size());
158        
159        for(ShipmentPackageStatusPK _pk: pks) {
160            _values.add(getValueFromPK(_pk));
161        }
162        
163        return _values;
164    }
165    
166    public ShipmentPackageStatusValue getValueFromPK(ShipmentPackageStatusPK pk)
167            throws PersistenceDatabaseException {
168        ShipmentPackageStatusValue _value;
169        
170        // See if we already have the entity in the session cache
171        ShipmentPackageStatus _entity = (ShipmentPackageStatus)session.getEntity(pk);
172        if(_entity == null)
173            _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getShipmentPackageStatusValue();
174        else
175            _value = _entity.getShipmentPackageStatusValue();
176        
177        return _value;
178    }
179    
180    public java.util.List<ShipmentPackageStatusValue> getValuesFromResultSet(ResultSet rs)
181            throws PersistenceDatabaseException {
182        java.util.List<ShipmentPackageStatusValue> _result = new ArrayList<>();
183        
184        try {
185            while(rs.next()) {
186                _result.add(getValueFromResultSet(rs));
187            }
188        } catch (SQLException se) {
189            throw new PersistenceDatabaseException(se);
190        }
191        
192        return _result;
193    }
194    
195    public ShipmentPackageStatusValue getValueFromResultSet(ResultSet rs)
196            throws PersistenceDatabaseException {
197        ShipmentPackageStatusValue _value;
198        
199        try {
200            Long shppckgst_shipmentpackagestatusid = rs.getLong(SHPPCKGST_SHIPMENTPACKAGESTATUSID);
201            ShipmentPackageStatusPK _pk = new ShipmentPackageStatusPK(shppckgst_shipmentpackagestatusid);
202            
203            // See if we already have the entity in the session cache
204            ShipmentPackageStatus _entity = (ShipmentPackageStatus)session.getEntity(_pk);
205            
206            if(_entity == null) {
207                Long shppckgst_shppckg_shipmentpackageid = rs.getLong(SHPPCKGST_SHPPCKG_SHIPMENTPACKAGEID);
208                if(rs.wasNull())
209                    shppckgst_shppckg_shipmentpackageid = null;
210                
211                Integer shppckgst_shipmentpackagelinesequence = rs.getInt(SHPPCKGST_SHIPMENTPACKAGELINESEQUENCE);
212                if(rs.wasNull())
213                    shppckgst_shipmentpackagelinesequence = null;
214                
215                _value = new ShipmentPackageStatusValue(_pk, new ShipmentPackagePK(shppckgst_shppckg_shipmentpackageid), shppckgst_shipmentpackagelinesequence);
216            } else
217                _value = _entity.getShipmentPackageStatusValue();
218        } catch (SQLException se) {
219            throw new PersistenceDatabaseException(se);
220        }
221        
222        return _value;
223    }
224    
225    public java.util.List<ShipmentPackageStatus> getEntitiesFromPKs(EntityPermission entityPermission, Collection<ShipmentPackageStatusPK> pks)
226            throws PersistenceDatabaseException {
227        java.util.List<ShipmentPackageStatus> _entities = new ArrayList<>(pks.size());
228        
229        for(ShipmentPackageStatusPK _pk: pks) {
230            _entities.add(getEntityFromPK(entityPermission, _pk));
231        }
232        
233        return _entities;
234    }
235    
236    public ShipmentPackageStatus getEntityFromValue(EntityPermission entityPermission, ShipmentPackageStatusValue value) {
237        return getEntityFromPK(entityPermission, value.getPrimaryKey());
238    }
239    
240    public ShipmentPackageStatus getEntityFromCache(ShipmentPackageStatusPK pk) {
241        ShipmentPackageStatusValue _value = (ShipmentPackageStatusValue)session.getValueCache().get(pk);
242    
243        return _value == null ? null : new ShipmentPackageStatus(_value, EntityPermission.READ_ONLY);
244    }
245    
246    public ShipmentPackageStatus getEntityFromPK(EntityPermission entityPermission, ShipmentPackageStatusPK pk)
247            throws PersistenceDatabaseException {
248        ShipmentPackageStatus _entity;
249        
250        // See if we already have the entity in the session cache
251        _entity = (ShipmentPackageStatus)session.getEntity(pk);
252        if(_entity != null) {
253            // If the requested permission is READ_WRITE, and the cached permission is
254            // READ_ONLY, then pretend that the cached object wasn't found, and create
255            // a new entity that is READ_WRITE.
256            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
257                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
258                    _entity = null;
259            }
260        }
261        
262        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
263            _entity = getEntityFromCache(pk);
264        }
265        
266        if(_entity == null) {
267            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
268            long _entityId = pk.getEntityId();
269            ResultSet _rs = null;
270            
271            try {
272                _ps.setLong(1, _entityId);
273                _rs = _ps.executeQuery();
274                if(_rs.next()) {
275                    _entity = getEntityFromResultSet(entityPermission, _rs);
276                }
277            } catch (SQLException se) {
278                throw new PersistenceDatabaseException(se);
279            } finally {
280                if(_rs != null) {
281                    try {
282                        _rs.close();
283                    } catch (SQLException se) {
284                        // do nothing
285                    }
286                }
287            }
288        }
289        
290        return _entity;
291    }
292    
293    public Set<ShipmentPackageStatusPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
294            throws PersistenceDatabaseException {
295        Set<ShipmentPackageStatusPK> _pks;
296        ResultSet _rs = null;
297        
298        try {
299            if(params.length != 0) {
300                Session.setQueryParams(ps, params);
301            }
302            
303            _rs = ps.executeQuery();
304            _pks = getPKsFromResultSetAsSet(_rs);
305            _rs.close();
306        } catch (SQLException se) {
307            throw new PersistenceDatabaseException(se);
308        } finally {
309            if(_rs != null) {
310                try {
311                    _rs.close();
312                } catch (SQLException se) {
313                    // do nothing
314                }
315            }
316        }
317        
318        return _pks;
319    }
320    
321    public java.util.List<ShipmentPackageStatusPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
322            throws PersistenceDatabaseException {
323        java.util.List<ShipmentPackageStatusPK> _pks;
324        ResultSet _rs = null;
325        
326        try {
327            if(params.length != 0) {
328                Session.setQueryParams(ps, params);
329            }
330            
331            _rs = ps.executeQuery();
332            _pks = getPKsFromResultSetAsList(_rs);
333            _rs.close();
334        } catch (SQLException se) {
335            throw new PersistenceDatabaseException(se);
336        } finally {
337            if(_rs != null) {
338                try {
339                    _rs.close();
340                } catch (SQLException se) {
341                    // do nothing
342                }
343            }
344        }
345        
346        return _pks;
347    }
348    
349    public ShipmentPackageStatusPK getPKFromQuery(PreparedStatement ps, final Object... params)
350            throws PersistenceDatabaseException {
351        ShipmentPackageStatusPK _pk = null;
352        ResultSet _rs = null;
353        
354        try {
355            if(params.length != 0) {
356                Session.setQueryParams(ps, params);
357            }
358            
359            _rs = ps.executeQuery();
360            if(_rs.next()) {
361                _pk = getPKFromResultSet(_rs);
362            }
363            _rs.close();
364        } catch (SQLException se) {
365            throw new PersistenceDatabaseException(se);
366        } finally {
367            if(_rs != null) {
368                try {
369                    _rs.close();
370                } catch (SQLException se) {
371                    // do nothing
372                }
373            }
374        }
375        
376        return _pk;
377    }
378    
379    public java.util.List<ShipmentPackageStatus> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
380            throws PersistenceDatabaseException {
381        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
382        
383        return getEntitiesFromQuery(entityPermission, ps, params);
384    }
385    
386    public java.util.List<ShipmentPackageStatus> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
387            throws PersistenceDatabaseException {
388        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
389        
390        return getEntitiesFromQuery(entityPermission, ps);
391    }
392    
393    public java.util.List<ShipmentPackageStatus> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
394            throws PersistenceDatabaseException {
395        PreparedStatement ps = session.prepareStatement(query.getSQL());
396        
397        return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray());
398    }
399    
400    public java.util.List<ShipmentPackageStatus> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
401            throws PersistenceDatabaseException {
402        java.util.List<ShipmentPackageStatus> _entities;
403        ResultSet _rs = null;
404        
405        try {
406            if(params.length != 0) {
407                Session.setQueryParams(ps, params);
408            }
409            
410            _rs = ps.executeQuery();
411            _entities = getEntitiesFromResultSet(entityPermission, _rs);
412            _rs.close();
413        } catch (SQLException se) {
414            throw new PersistenceDatabaseException(se);
415        } finally {
416            if(_rs != null) {
417                try {
418                    _rs.close();
419                } catch (SQLException se) {
420                    // do nothing
421                }
422            }
423        }
424        
425        return _entities;
426    }
427    
428    public ShipmentPackageStatus getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
429            throws PersistenceDatabaseException {
430        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
431        
432        return getEntityFromQuery(entityPermission, ps, params);
433    }
434    
435    public ShipmentPackageStatus getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
436            throws PersistenceDatabaseException {
437        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
438        
439        return getEntityFromQuery(entityPermission, ps);
440    }
441    
442    public ShipmentPackageStatus getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
443            throws PersistenceDatabaseException {
444        PreparedStatement ps = session.prepareStatement(query.getSQL());
445        
446        return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray());
447    }
448    
449    public ShipmentPackageStatus getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
450            throws PersistenceDatabaseException {
451        ShipmentPackageStatus _entity = null;
452        ResultSet _rs = null;
453        
454        try {
455            if(params.length != 0) {
456                Session.setQueryParams(ps, params);
457            }
458            
459            _rs = ps.executeQuery();
460            if(_rs.next()) {
461                _entity = getEntityFromResultSet(entityPermission, _rs);
462            }
463            _rs.close();
464        } catch (SQLException se) {
465            throw new PersistenceDatabaseException(se);
466        } finally {
467            if(_rs != null) {
468                try {
469                    _rs.close();
470                } catch (SQLException se) {
471                    // do nothing
472                }
473            }
474        }
475        
476        return _entity;
477    }
478    
479    public java.util.List<ShipmentPackageStatus> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
480            throws PersistenceDatabaseException {
481        java.util.List<ShipmentPackageStatus> _result = new ArrayList<>();
482        
483        try {
484            while(rs.next()) {
485                _result.add(getEntityFromResultSet(entityPermission, rs));
486            }
487        } catch (SQLException se) {
488            throw new PersistenceDatabaseException(se);
489        }
490        
491        return _result;
492    }
493    
494    public ShipmentPackageStatus getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
495            throws PersistenceDatabaseException {
496        ShipmentPackageStatus _entity;
497        
498        try {
499            Long shppckgst_shipmentpackagestatusid = rs.getLong(SHPPCKGST_SHIPMENTPACKAGESTATUSID);
500            ShipmentPackageStatusPK _pk = new ShipmentPackageStatusPK(shppckgst_shipmentpackagestatusid);
501            
502            // See if we already have the entity in the session cache
503            _entity = (ShipmentPackageStatus)session.getEntity(_pk);
504            if(_entity != null) {
505                // If the requested permission is READ_WRITE, and the cached permission is
506                // READ_ONLY, then pretend that the cached object wasn't found, and create
507                // a new entity that is READ_WRITE.
508                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
509                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
510                        _entity = null;
511                }
512            }
513            boolean foundInSessionCache = _entity != null;
514            
515            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
516                _entity = getEntityFromCache(_pk);
517            }
518            
519            if(_entity == null) {
520                Long shppckgst_shppckg_shipmentpackageid = rs.getLong(SHPPCKGST_SHPPCKG_SHIPMENTPACKAGEID);
521                if(rs.wasNull())
522                    shppckgst_shppckg_shipmentpackageid = null;
523                
524                Integer shppckgst_shipmentpackagelinesequence = rs.getInt(SHPPCKGST_SHIPMENTPACKAGELINESEQUENCE);
525                if(rs.wasNull())
526                    shppckgst_shipmentpackagelinesequence = null;
527                
528                ShipmentPackageStatusValue _value = new ShipmentPackageStatusValue(_pk, shppckgst_shppckg_shipmentpackageid == null? null: new ShipmentPackagePK(shppckgst_shppckg_shipmentpackageid), shppckgst_shipmentpackagelinesequence);
529                _entity = new ShipmentPackageStatus(_value, entityPermission);
530            }
531            
532            if(!foundInSessionCache) {
533                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
534                    session.putReadOnlyEntity(_pk, _entity);
535                    session.getValueCache().put(_entity.getShipmentPackageStatusValue());
536                } else {
537                    session.putReadWriteEntity(_pk, _entity);
538                }
539            }
540        } catch (SQLException se) {
541            throw new PersistenceDatabaseException(se);
542        }
543        
544        return _entity;
545    }
546    
547    public ShipmentPackageStatus create(ShipmentPackage shipmentPackage, Integer shipmentPackageLineSequence)
548            throws PersistenceDatabaseException, PersistenceNotNullException {
549        return create(shipmentPackage == null ? null : shipmentPackage.getPrimaryKey(), shipmentPackageLineSequence);
550    }
551    
552    private void bindForCreate(PreparedStatement _ps, ShipmentPackageStatusValue _value)
553            throws SQLException {
554        _ps.setLong(1, _value.getEntityId());
555        
556        ShipmentPackagePK shppckgst_shppckg_shipmentpackageid = _value.getShipmentPackagePK();
557        if(shppckgst_shppckg_shipmentpackageid == null)
558            _ps.setNull(2, Types.BIGINT);
559        else
560            _ps.setLong(2, shppckgst_shppckg_shipmentpackageid.getEntityId());
561            
562        Integer shppckgst_shipmentpackagelinesequence = _value.getShipmentPackageLineSequence();
563        if(shppckgst_shipmentpackagelinesequence == null)
564            _ps.setNull(3, Types.INTEGER);
565        else
566            _ps.setInt(3, shppckgst_shipmentpackagelinesequence);
567            
568    }
569    
570    public ShipmentPackageStatus create(ShipmentPackagePK shipmentPackagePK, Integer shipmentPackageLineSequence)
571            throws PersistenceDatabaseException, PersistenceNotNullException {
572        ShipmentPackageStatusPK _pk = getNextPK();
573        ShipmentPackageStatusValue _value = new ShipmentPackageStatusValue(_pk, shipmentPackagePK, shipmentPackageLineSequence);
574        
575        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
576        
577        try {
578            bindForCreate(_ps, _value);
579            
580            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
581                int _count = _ps.executeUpdate();
582                
583                if(_count != 1) {
584                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
585                }
586            } else {
587                 _ps.executeUpdate();
588            }
589            
590            session.getValueCache().put(_value);
591        } catch (SQLException se) {
592            throw new PersistenceDatabaseException(se);
593        }
594        
595        ShipmentPackageStatus _entity = new ShipmentPackageStatus(_value, EntityPermission.READ_ONLY);
596        session.putReadOnlyEntity(_pk, _entity);
597        
598        return _entity;
599    }
600    
601    public void create(Collection<ShipmentPackageStatusValue> _values)
602            throws PersistenceDatabaseException, PersistenceNotNullException {
603        int _size = _values.size();
604        
605        if(_size > 0) {
606            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
607            List<ShipmentPackageStatusValue> _cacheValues = new ArrayList<>(_size);
608            
609            try {
610                for(ShipmentPackageStatusValue _value : _values) {
611                    _value.setEntityId(entityIdGenerator.getNextEntityId());
612                    bindForCreate(_ps, _value);
613                    
614                    _ps.addBatch();
615                    
616                    _cacheValues.add(_value);
617                }
618                
619                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
620                    int[] _counts = _ps.executeBatch();
621                    
622                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
623                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
624                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
625                        }
626                    }
627                } else {
628                     _ps.executeBatch();
629                }
630                
631                _ps.clearBatch();
632            } catch (SQLException se) {
633                throw new PersistenceDatabaseException(se);
634            }
635            
636            _cacheValues.forEach((_cacheValue) -> {
637                ShipmentPackageStatus _cacheEntity = new ShipmentPackageStatus(_cacheValue, EntityPermission.READ_ONLY);
638                
639                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
640            });
641        }
642    }
643    
644    private boolean bindForStore(PreparedStatement _ps, ShipmentPackageStatusValue _value)
645            throws SQLException {
646        boolean _hasBeenModified = _value.hasBeenModified();
647        
648        if(_hasBeenModified) {
649            ShipmentPackagePK shppckgst_shppckg_shipmentpackageid = _value.getShipmentPackagePK();
650            if(shppckgst_shppckg_shipmentpackageid == null)
651                _ps.setNull(1, Types.BIGINT);
652            else
653                _ps.setLong(1, shppckgst_shppckg_shipmentpackageid.getEntityId());
654            
655            Integer shppckgst_shipmentpackagelinesequence = _value.getShipmentPackageLineSequence();
656            if(shppckgst_shipmentpackagelinesequence == null)
657                _ps.setNull(2, Types.INTEGER);
658            else
659                _ps.setInt(2, shppckgst_shipmentpackagelinesequence);
660            
661            _ps.setLong(3, _value.getPrimaryKey().getEntityId());
662            
663            _value.clearHasBeenModified();
664        }
665        
666        return _hasBeenModified;
667    }
668    
669    @Override
670    public void store(ShipmentPackageStatus entity)
671            throws PersistenceDatabaseException {
672        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
673        
674        try {
675            ShipmentPackageStatusValue _value = entity.getShipmentPackageStatusValue();
676            
677            if(bindForStore(_ps, _value)) {
678                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
679                    int _count = _ps.executeUpdate();
680                    
681                    if(_count != 1) {
682                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
683                    }
684                } else {
685                     _ps.executeUpdate();
686                }
687                
688                session.getValueCache().put(_value);
689            }
690        } catch (SQLException se) {
691            throw new PersistenceDatabaseException(se);
692        }
693    }
694    
695    @Override
696    public void store(Collection<ShipmentPackageStatus> entities)
697            throws PersistenceDatabaseException {
698        if(entities.size() > 0) {
699            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
700            int _modifiedEntities = 0;
701            
702            try {
703                for(ShipmentPackageStatus entity : entities) {
704                    if(bindForStore(_ps, entity.getShipmentPackageStatusValue())) {
705                        _ps.addBatch();
706                        _modifiedEntities++;
707                    }
708                }
709                
710                if(_modifiedEntities != 0) {
711                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
712                        int[] _counts = _ps.executeBatch();
713                        
714                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
715                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
716                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
717                            }
718                        }
719                    } else {
720                         _ps.executeBatch();
721                    }
722                    
723                    _ps.clearBatch();
724                    
725                    entities.forEach((entity) -> {
726                        session.getValueCache().put(entity.getShipmentPackageStatusValue());
727                    });
728                }
729            } catch (SQLException se) {
730                throw new PersistenceDatabaseException(se);
731            }
732        }
733    }
734    
735    @Override
736    public void remove(ShipmentPackageStatus entity)
737            throws PersistenceDatabaseException {
738        remove(entity.getPrimaryKey());
739    }
740    
741    @Override
742    public void remove(ShipmentPackageStatusPK pk)
743            throws PersistenceDatabaseException {
744        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
745        long _entityId = pk.getEntityId();
746        
747        try {
748            _ps.setLong(1, _entityId);
749            
750            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
751                int _count = _ps.executeUpdate();
752                
753                if(_count != 1) {
754                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
755                }
756            } else {
757                 _ps.executeUpdate();
758            }
759            
760            session.getValueCache().remove(pk);
761        } catch (SQLException se) {
762            throw new PersistenceDatabaseException(se);
763        }
764        
765        session.removed(pk, false);
766    }
767    
768    @Override
769    public void remove(Collection<ShipmentPackageStatusPK> pks)
770            throws PersistenceDatabaseException {
771        if(pks.size() > 0) {
772            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
773            int _modifiedEntities = 0;
774            
775            try {
776                for(ShipmentPackageStatusPK pk : pks) {
777                    long _entityId = pk.getEntityId();
778                    
779                    _ps.setLong(1, _entityId);
780                    
781                    _ps.addBatch();
782                    _modifiedEntities++;
783                }
784                
785                if(_modifiedEntities != 0) {
786                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
787                        int[] _counts = _ps.executeBatch();
788                        
789                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
790                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
791                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
792                            }
793                        }
794                    } else {
795                        _ps.executeBatch();
796                    }
797                    
798                    _ps.clearBatch();
799                    
800                    pks.forEach((pk) -> {
801                        session.getValueCache().remove(pk);
802                    });
803                }
804            } catch (SQLException se) {
805                throw new PersistenceDatabaseException(se);
806            }
807            
808            pks.forEach((pk) -> {
809                session.removed(pk, true);
810            });
811        }
812    }
813    
814    @Override
815    public boolean validPK(ShipmentPackageStatusPK pk)
816            throws PersistenceDatabaseException {
817        boolean valid = false;
818        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
819        ResultSet _rs = null;
820        
821        try {
822            _ps.setLong(1, pk.getEntityId());
823            
824            _rs = _ps.executeQuery();
825            if(_rs.next()) {
826                long _count = _rs.getLong(1);
827                if(_rs.wasNull())
828                    _count = 0;
829                
830                if(_count == 1)
831                    valid = true;
832            }
833        } catch (SQLException se) {
834            throw new PersistenceDatabaseException(se);
835        } finally {
836            if(_rs != null) {
837                try {
838                    _rs.close();
839                } catch (SQLException se) {
840                    // do nothing
841                }
842            }
843        }
844        
845        return valid;
846    }
847    
848}