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