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