Skip to content

Commit fb6a225

Browse files
Daogen release helper
1 parent 61658ca commit fb6a225

31 files changed

+1077
-38
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.fugerit.java.core.cfg.xml;
2+
3+
public class FactoryCatalog extends CustomListCatalogConfig<FactoryType, ListMapConfig<FactoryType>> {
4+
5+
/**
6+
* Default configuration element for a data list
7+
*/
8+
public static final String ATT_TAG_DATA_LIST = "factory";
9+
10+
/**
11+
* Default configuration entry for a data entry
12+
*/
13+
public static final String ATT_TAG_DATA = "data";
14+
15+
/**
16+
*
17+
*/
18+
private static final long serialVersionUID = -6578973354062035200L;
19+
20+
21+
public FactoryCatalog() {
22+
super(ATT_TAG_DATA_LIST, ATT_TAG_DATA);
23+
this.getGeneralProps().setProperty( ATT_TYPE , FactoryType.class.getName() );
24+
}
25+
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.fugerit.java.core.cfg.xml;
2+
3+
public class FactoryType extends BasicIdConfigType {
4+
5+
/**
6+
*
7+
*/
8+
private static final long serialVersionUID = 4156411727576276222L;
9+
10+
private String type;
11+
12+
private String info;
13+
14+
public String getType() {
15+
return type;
16+
}
17+
18+
public void setType(String type) {
19+
this.type = type;
20+
}
21+
22+
public String getInfo() {
23+
return info;
24+
}
25+
26+
public void setInfo(String info) {
27+
this.info = info;
28+
}
29+
30+
}

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/PropertyHolder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
public class PropertyHolder extends BasicIdConfigType {
1616

1717
public final static String MODE_CLASS_LOADER = "classloader";
18+
public final static String MODE_CL = "cl";
1819

1920
public final static String MODE_FILE = "file";
2021

@@ -77,7 +78,7 @@ private static void loadWorker( InputStream is, Properties props, boolean xml )
7778

7879
public static Properties load( String mode, String path, String xml ) throws IOException {
7980
Properties props = new Properties();
80-
if ( MODE_CLASS_LOADER.equalsIgnoreCase( mode ) ) {
81+
if ( MODE_CLASS_LOADER.equalsIgnoreCase( mode ) || MODE_CL.equalsIgnoreCase( mode ) ) {
8182
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( path ) ) {
8283
loadWorker( is , props, BooleanUtils.isTrue( xml ) );
8384
} catch (Exception e) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.fugerit.java.core.db.daogen;
2+
3+
import java.io.Serializable;
4+
import java.math.BigDecimal;
5+
6+
public class BaseIdFinder implements Serializable {
7+
8+
/**
9+
*
10+
*/
11+
private static final long serialVersionUID = 8019457921226089217L;
12+
13+
private BigDecimal id;
14+
15+
public BigDecimal getId() {
16+
return id;
17+
}
18+
19+
public void setId(BigDecimal id) {
20+
this.id = id;
21+
}
22+
23+
public void setId(long id) {
24+
this.id = new BigDecimal( id );
25+
}
26+
27+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package org.fugerit.java.core.db.daogen;
2+
3+
import java.io.Serializable;
4+
import java.math.BigDecimal;
5+
import java.sql.Connection;
6+
import java.sql.PreparedStatement;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
import java.util.List;
11+
import java.util.concurrent.TimeUnit;
12+
13+
import org.fugerit.java.core.db.dao.DAOException;
14+
import org.fugerit.java.core.db.dao.DAOHelper;
15+
import org.fugerit.java.core.db.dao.FieldFactory;
16+
import org.fugerit.java.core.db.dao.FieldList;
17+
import org.fugerit.java.core.db.dao.RSExtractor;
18+
import org.fugerit.java.core.log.BasicLogObject;
19+
20+
public class BasicDAOHelper<T> extends BasicLogObject implements Serializable {
21+
22+
/**
23+
*
24+
*/
25+
private static final long serialVersionUID = -2430439741412903230L;
26+
27+
private final static long LOG_TIME_THRESHOLD = TimeUnit.SECONDS.toMillis( 10 );
28+
29+
public static String fieldListToString( FieldList fl ) {
30+
StringBuffer buffer = new StringBuffer();
31+
buffer.append( "[" );
32+
if ( fl.size() > 0 ) {
33+
buffer.append( fl.getField( 0 ).toString() );
34+
for ( int k=1; k<fl.size(); k++ ) {
35+
buffer.append( "," );
36+
buffer.append( fl.getField( k ).toString() );
37+
}
38+
}
39+
buffer.append( "]" );
40+
return buffer.toString();
41+
}
42+
43+
private DAOContext daoContext;
44+
45+
public BasicDAOHelper( DAOContext daoContext) {
46+
this.daoContext =daoContext;
47+
}
48+
49+
public FieldList newFieldList() {
50+
return new FieldList( new FieldFactory() );
51+
}
52+
53+
public void loadAllHelper( List<T> l, SelectHelper query, RSExtractor<T> re ) throws DAOException {
54+
this.loadAllHelper( l, query.getQuery().toString(), query.getFields(), re );
55+
}
56+
57+
58+
public void loadAllHelper( List<T> l, String query, FieldList fields, RSExtractor<T> re ) throws DAOException {
59+
try {
60+
BasicDAOHelper<T> log = this;
61+
log.getLogger().debug( "loadAll START list : '{}' ", l.size() );
62+
log.getLogger().debug( "loadAll fields : '{}'", fields.size() );
63+
log.getLogger().debug( "loadAll RSExtractor : '{}'", re);
64+
long startTime = System.currentTimeMillis();
65+
Connection conn = this.daoContext.getConnection();
66+
long step1 = System.currentTimeMillis()-startTime;
67+
int i=0;
68+
try ( PreparedStatement ps = conn.prepareStatement( query ) ) {
69+
long step2 = System.currentTimeMillis()-startTime;
70+
DAOHelper.setAll( ps, fields , log );
71+
long step3 = System.currentTimeMillis()-startTime;
72+
try ( ResultSet rs = ps.executeQuery() ) {
73+
long step4 = System.currentTimeMillis()-startTime;
74+
while (rs.next()) {
75+
l.add( re.extractNext( rs ) );
76+
i++;
77+
}
78+
long elapsed = System.currentTimeMillis()-startTime;
79+
if ( elapsed > LOG_TIME_THRESHOLD ) {
80+
String message = "BasicDAOHelper LOG_TIME_THRESHOLD="+LOG_TIME_THRESHOLD+", elapsed:"+elapsed+", rsCount:"+i;
81+
message+= " step1:"+step1+" step2:"+step2+" step3:"+step3+" step4:"+step4;
82+
log.getLogger().info( message );
83+
}
84+
}
85+
} catch (SQLException e) {
86+
throw (new DAOException( e.getMessage()+"[query:"+query+",record:"+i+"]", e ));
87+
}
88+
log.getLogger().debug("loadAll END list : '{}'", l.size());
89+
} catch (DAOException e) {
90+
throw new DAOException( e );
91+
}
92+
}
93+
94+
95+
public int update( QueryHelper queryHelper ) throws DAOException {
96+
int res = 0;
97+
try {
98+
BasicDAOHelper<T> log = this;
99+
String query = queryHelper.getQueryContent();
100+
FieldList fields = queryHelper.getFields();
101+
log.getLogger().debug( "update START list : '{}' ", query );
102+
log.getLogger().debug( "update fields : '{}'", fields.size() );
103+
Connection conn = this.daoContext.getConnection();
104+
int i=0;
105+
try ( PreparedStatement ps = conn.prepareStatement( query ) ) {
106+
DAOHelper.setAll( ps, fields , log );
107+
res = ps.executeUpdate();
108+
} catch (SQLException e) {
109+
throw (new DAOException( e.getMessage()+"[query:"+query+",record:"+i+"]", e ));
110+
}
111+
log.getLogger().debug("update END res : '{}'", res );
112+
} catch (DAOException e) {
113+
throw new DAOException( e );
114+
}
115+
return res;
116+
}
117+
118+
public BigDecimal newSequenceValue( String sequence ) throws DAOException {
119+
BigDecimal id = null;
120+
String sql = " SELECT "+sequence+".NEXTVAL FROM DUAL";
121+
this.getLogger().info( "newSequenceValue() sql > "+sql );
122+
try ( Statement stm = this.daoContext.getConnection().createStatement();
123+
ResultSet rs = stm.executeQuery( sql ) ) {
124+
if ( rs.next() ) {
125+
id = rs.getBigDecimal( 1 );
126+
}
127+
} catch (Exception e) {
128+
throw new DAOException( e );
129+
}
130+
return id;
131+
}
132+
133+
public SelectHelper newSelectHelper( String tableName ) {
134+
SelectHelper query = new SelectHelper( tableName , this.newFieldList() );
135+
query.initSelectEntity();
136+
return query;
137+
}
138+
139+
public InsertHelper newInsertHelper( String tableName ) {
140+
InsertHelper query = new InsertHelper( tableName , this.newFieldList() );
141+
return query;
142+
}
143+
144+
public UpdateHelper newUpdateHelper( String tableName ) {
145+
UpdateHelper query = new UpdateHelper( tableName , this.newFieldList() );
146+
return query;
147+
}
148+
149+
public DeleteHelper newDeleteHelper( String tableName ) {
150+
DeleteHelper query = new DeleteHelper( tableName , this.newFieldList() );
151+
return query;
152+
}
153+
154+
155+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.fugerit.java.core.db.daogen;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.fugerit.java.core.util.result.BasicResult;
7+
8+
public class BasicDaoResult<T> extends BasicResult {
9+
10+
public static final int RESULT_NODATAFOUND = -3;
11+
public static final int RESULT_NOT_SET = Integer.MIN_VALUE;
12+
13+
public BasicDaoResult(int resultCode) {
14+
super(resultCode);
15+
this.list = new ArrayList<T>();
16+
}
17+
18+
public BasicDaoResult() {
19+
this( RESULT_NOT_SET );
20+
}
21+
22+
private List<T> list;
23+
24+
public List<T> getList() {
25+
return list;
26+
}
27+
28+
private String resultDescription;
29+
30+
public String getResultDescription() {
31+
return this.resultDescription;
32+
}
33+
34+
public void setResultDescription( String v ) {
35+
this.resultDescription = v;
36+
}
37+
38+
public void evaluateResultFromList() {
39+
if ( this.getList().isEmpty() ) {
40+
this.setResultCode( RESULT_NODATAFOUND );
41+
} else {
42+
this.setResultCode( RESULT_CODE_OK );
43+
}
44+
}
45+
46+
public void setResult( int resultCode, String resultDescription ) {
47+
this.setResultCode( resultCode );
48+
this.setResultDescription( resultDescription );
49+
}
50+
51+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.fugerit.java.core.db.daogen;
2+
3+
import java.math.BigDecimal;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import org.fugerit.java.core.db.dao.DAOException;
8+
import org.fugerit.java.core.db.dao.RSExtractor;
9+
10+
public class BasicDataFacade<T> extends BasicHelper {
11+
12+
/**
13+
*
14+
*/
15+
private static final long serialVersionUID = 5321073652254215522L;
16+
17+
private String tableName;
18+
19+
private RSExtractor<T> rse;
20+
21+
public String getTableName() {
22+
return tableName;
23+
}
24+
25+
public RSExtractor<T> getRse() {
26+
return rse;
27+
}
28+
29+
public BasicDataFacade(String tableName, RSExtractor<T> rse) {
30+
super();
31+
this.tableName = tableName;
32+
this.rse = rse;
33+
}
34+
35+
public T loadById( DAOContext context, BigDecimal id ) throws DAOException {
36+
T model = null;
37+
BasicDAOHelper<T> daoHelper = new BasicDAOHelper<T>( context );
38+
SelectHelper query = daoHelper.newSelectHelper( this.getTableName() );
39+
query.andEqualParam( "id" , id );
40+
List<T> list = new ArrayList<>();
41+
daoHelper.loadAllHelper( list , query, this.getRse() );
42+
if ( list.size() == 1 ) {
43+
model = list.get( 0 );
44+
}
45+
return model;
46+
}
47+
48+
public BasicDaoResult<T> loadAll( DAOContext context ) throws DAOException {
49+
BasicDaoResult<T> result = new BasicDaoResult<T>();
50+
BasicDAOHelper<T> daoHelper = new BasicDAOHelper<T>( context );
51+
SelectHelper query = daoHelper.newSelectHelper( this.getTableName() );
52+
daoHelper.loadAllHelper( result.getList() , query, this.getRse() );
53+
result.evaluateResultFromList();
54+
return result;
55+
}
56+
57+
public void evaluteSqlUpdateResult( int res, T model, BasicDaoResult<T> result ) {
58+
if ( res > 0 ) {
59+
result.setResult( BasicDaoResult.RESULT_CODE_OK , "Operation OK" );
60+
if ( model != null ) {
61+
result.getList().add( model );
62+
}
63+
} else {
64+
result.setResult( BasicDaoResult.RESULT_CODE_KO , "Operation KO" );
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)