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