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