1
2
3
4 package gboat2.base.dao.impl;
5
6 import gboat2.base.core.annotation.Relations;
7 import gboat2.base.core.dao.IBaseDAO;
8 import gboat2.base.core.dao.Page;
9 import gboat2.base.core.dao.PageBean;
10 import gboat2.base.core.dao.QuerySupport;
11 import gboat2.base.core.model.Parameter;
12 import gboat2.base.core.service.IParameterService;
13
14 import java.io.Serializable;
15 import java.lang.reflect.Field;
16 import java.lang.reflect.Method;
17 import java.math.BigDecimal;
18 import java.math.BigInteger;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.persistence.Entity;
28 import javax.persistence.Enumerated;
29
30 import org.hibernate.Hibernate;
31 import org.hibernate.Query;
32 import org.hibernate.ScrollMode;
33 import org.hibernate.ScrollableResults;
34 import org.hibernate.Session;
35 import org.hibernate.dialect.Dialect;
36 import org.hibernate.impl.SessionImpl;
37 import org.osgi.framework.Bundle;
38 import org.osgi.framework.BundleContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.orm.hibernate3.HibernateCallback;
42 import org.springframework.osgi.context.BundleContextAware;
43 import org.springframework.transaction.annotation.Transactional;
44
45
46
47
48
49 @Transactional
50 public class BaseDAO extends GboatHibernateDaoSupport implements IBaseDAO, BundleContextAware {
51 private static Logger logger = LoggerFactory.getLogger(BaseDAO.class);
52 private BundleContext bundleContext;
53
54
55
56
57 @Override
58 public void delete(Object object) {
59 getHibernateTemplate().delete(object);
60 }
61
62
63
64
65 @Override
66 public void delete(Class<?> clazz, Serializable id) {
67 Object object = get(clazz, id);
68 if (object != null) {
69 getHibernateTemplate().delete(object);
70 }
71 }
72
73
74
75
76 @Override
77 public Integer deleteByQuery(final String queryString, final Map<String, Object> parameters) {
78 return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
79
80 public Integer doInHibernate(Session session) {
81 Query query = session.createQuery(queryString);
82
83 String[] keys = query.getNamedParameters();
84
85 if (keys != null && parameters != null) {
86 for (String key : keys) {
87 if (!parameters.containsKey(key)) {
88 throw new RuntimeException("没有设置参数" + key + "的值");
89 }
90 Object value = parameters.get(key);
91 query.setParameter(key, value);
92 }
93 }
94 return Integer.valueOf(query.executeUpdate());
95 }
96 });
97 }
98
99
100
101
102 @Override
103 public Object get(Class<?> clazz, Serializable id) {
104 return getHibernateTemplate().get(clazz, id);
105 }
106
107
108
109
110 @Override
111 public Object get(String queryString, Map<String, Object> params) {
112 Query q = createHQLQuery(queryString, params);
113 List<?> list = q.list();
114 if (list != null && !list.isEmpty()) {
115 return list.get(0);
116 }
117 return null;
118 }
119
120
121
122
123 @Override
124 public Object get(String[][] params) {
125 List<?> objs = (List<?>) queryList(params);
126 if (objs.size() > 0) {
127 return objs.get(0);
128 } else {
129 return null;
130 }
131 }
132
133
134
135
136 @Override
137 public Object get(Map<String, Object> params) {
138 List<?> objs = (List<?>) queryList(params);
139 if (objs.size() > 0) {
140 return objs.get(0);
141 } else {
142 return null;
143 }
144 }
145
146
147
148
149
150
151
152
153 @Override
154 public Page<Object> getPage(String hql, int page, int pagesize, Map<String, Object> params) {
155 PageBean pageBean = new PageBean(page, pagesize);
156
157 Query query = createHQLQuery(hql, params);
158
159 int cnt = getCountNumber(hql, params);
160 pageBean.setCount(cnt);
161 if (pageBean.getCount() == 0) {
162 return new Page<Object>(new ArrayList<Object>(), pageBean);
163 }
164 if (pagesize != -1) {
165 query.setMaxResults(pagesize);
166 }
167 query.setFirstResult(pageBean.getStartNo() - 1);
168 List<?> list = query.list();
169 return new Page<Object>(list, pageBean);
170 }
171
172
173
174
175 @Override
176 public Page<Object> getPage(Map<String, Object> queryParams) {
177
178 Object setedPage = queryParams.get(QuerySupport.PARAM_PAGE);
179 int page = 1;
180 if (setedPage instanceof String) {
181 try {
182 page = Integer.parseInt((String) setedPage);
183 } catch (NumberFormatException e) {
184 page = 1;
185 }
186 } else if (setedPage instanceof String[]) {
187 if (((String[]) setedPage).length > 0) {
188 try {
189 page = Integer.parseInt(((String[]) setedPage)[0]);
190 } catch (NumberFormatException e) {
191 page = 1;
192 }
193 }
194 } else if (setedPage instanceof Integer) {
195 page = (Integer) setedPage;
196 }
197
198 Object pageSizeParam = queryParams.get(QuerySupport.PARAM_PAGESIZE);
199 int pagesize = QuerySupport.PAGESIZE_DEFAULT;
200 if (pageSizeParam instanceof String) {
201 try {
202 pagesize = Integer.parseInt((String) pageSizeParam);
203 } catch (NumberFormatException e) {
204 pagesize = QuerySupport.PAGESIZE_DEFAULT;
205 }
206 } else if (pageSizeParam instanceof String[]) {
207 String[] pageSizes = (String[]) pageSizeParam;
208 if (pageSizes.length > 0) {
209 try {
210 pagesize = Integer.parseInt(pageSizes[0]);
211 } catch (NumberFormatException e) {
212 e.printStackTrace();
213 }
214 }
215 } else if (pageSizeParam instanceof Integer) {
216 pagesize = (Integer) pageSizeParam;
217 }
218
219
220 if(pageSizeParam == null){
221 IParameterService service = (IParameterService)bundleContext.getService(bundleContext.getServiceReference(IParameterService.class.getName()));
222 String jsonOfParameter = "{'desc':'设置全局分页参数','groupName':'G2平台参数','code':'CODE_PAGESIZE','name':'全局分页数据','value':'20'}";
223 Parameter initOrGetPlatformParameter = service.getParameter(jsonOfParameter);
224 String parameterValue = initOrGetPlatformParameter.getValue();
225 try {
226 Integer platform_pagesize = Integer.valueOf(parameterValue);
227 pagesize = platform_pagesize;
228 } catch (Exception e) {
229 logger.info("platform_pagesize is invalid, if you do not need this parameter , ignore this ;other,check configration and change value to right");
230 }
231 }
232
233 int sizeForPageBean = pagesize;
234 if (pagesize == -1) {
235 sizeForPageBean = Integer.MAX_VALUE;
236 }
237 PageBean pageBean = new PageBean(page, sizeForPageBean);
238
239
240 GenerateQL qp = new GenerateQL();
241 Class<?> tableClass = qp.getTableClass(queryParams);
242 String hql;
243 Query query = null;
244 int cnt = 0;
245 if (tableClass.isAnnotationPresent(Entity.class)) {
246 hql = qp.generateHQL(queryParams, getDialect());
247 query = createHQLQuery(hql, convertParamsForPO(queryParams,tableClass));
248 } else if (tableClass.isAnnotationPresent(Relations.class)) {
249 hql = qp.generateSQL(queryParams, getDialect());
250 query = createSQLQuery(hql, convertParamsForVO(queryParams));
251 } else {
252 throw new RuntimeException(tableClass.getName() + " doesn't support by query as domain class.");
253 }
254
255 ScrollableResults scrollableResults = query.scroll(ScrollMode.SCROLL_SENSITIVE);
256 scrollableResults.last();
257
258 cnt = scrollableResults.getRowNumber() + 1;
259
260 pageBean.setCount(cnt);
261 pageBean.setCurrentPageParams(GenerateQL.getQueryParamsAsUrlString(queryParams));
262 if (pageBean.getCount() == 0) {
263 return new Page<Object>(new ArrayList<Object>(), pageBean);
264 }
265 if (pagesize != -1) {
266 query.setMaxResults(pagesize);
267 }
268 query.setFirstResult(pageBean.getStartNo() - 1);
269 @SuppressWarnings("unchecked")
270 List<Object> result = query.list();
271
272 if (tableClass.isAnnotationPresent(Relations.class)) {
273
274 List<Object> converted = convertResultToVO(tableClass, result);
275 result = converted;
276 }
277
278 return new Page<Object>(result, pageBean);
279 }
280
281 private Map<String,Object> convertParamsForPO(Map<String,Object> params, Class<?> poClass){
282 Method[] methods = poClass.getMethods();
283 String prop,key;
284 Object value;
285 Method valueOfMethod;
286 for (Method method : methods) {
287 if ((method.getAnnotation(Enumerated.class)) != null) {
288 char firstChar = Character.toLowerCase(method.getName().charAt(3));
289 prop = firstChar + method.getName().substring(4);
290 key = getKey(params,prop);
291 if(key!=null){
292 value = params.get(key);
293 if(value instanceof String[] && ((String[])value).length==1){
294 value = ((String[])value)[0];
295 try {
296 valueOfMethod = method.getReturnType().getMethod("valueOf", new Class[]{Class.class,String.class});
297 if(valueOfMethod!=null){
298 params.put(key, valueOfMethod.invoke(method.getReturnType(), new Object[]{method.getReturnType(),value}));
299 }
300 } catch (Exception e) {
301 e.printStackTrace();
302 }
303 }else if(value instanceof String){
304 try {
305 valueOfMethod = method.getReturnType().getMethod("valueOf", new Class[]{Class.class,String.class});
306 if(valueOfMethod!=null){
307 params.put(key, valueOfMethod.invoke(method.getReturnType(), new Object[]{method.getReturnType(),value}));
308 }
309 } catch (Exception e) {
310 e.printStackTrace();
311 }
312 }else if(value!=null&&!(value instanceof Enum)){
313 throw new RuntimeException("Enum should be dealed as String @ gboat2");
314 }
315 }
316 }
317 }
318 return params;
319 }
320
321 private String getKey (Map<String,Object> params,String keyPrefix){
322 for(String key : params.keySet()){
323 if(key.startsWith("_")&&key.endsWith(keyPrefix)){
324 return key;
325 }
326 }
327 return null;
328 }
329
330
331
332
333
334
335 @SuppressWarnings("rawtypes")
336 private Map<String,Object> convertParamsForVO(Map<String,Object> params){
337 Object value;
338 for(String key : params.keySet()){
339 value = params.get(key);
340 if(value instanceof Enum){
341 params.put(key, ((Enum)value).name());
342 }else if(value instanceof String[]){
343 value = ((String[])value)[0];
344 params.put(key, value);
345 }
346 }
347
348 return params;
349 }
350
351
352
353
354 @Override
355 public Page<Object> getPage(String[][] params) {
356 Map<String, Object> map = new HashMap<String, Object>();
357 String[] item;
358
359 for (int i = 0; i < params.length; i++) {
360 item = params[i];
361 if (item.length == 1) {
362 map.put(QuerySupport.PARAM_TABLENAME, item[0]);
363 } else if (item.length == 2) {
364 map.put(item[0], item[1]);
365 } else {
366 throw new RuntimeException("the parameter for getPage(String[][] params) is invalid.");
367 }
368 }
369 return getPage(map);
370 }
371
372
373
374
375 @Override
376 public List<?> queryList(Map<String, Object> queryParams) {
377 List<?> result = null;
378
379 GenerateQL qp = new GenerateQL();
380 Class<?> tableClass = qp.getTableClass(queryParams);
381 String hql;
382 Query query = null;
383 if (tableClass.isAnnotationPresent(Entity.class)) {
384 result = new LinkedList<Object>();
385 hql = qp.generateHQL(queryParams, getDialect());
386 query = createHQLQuery(hql, convertParamsForPO(queryParams,tableClass));
387
388 if (queryParams.containsKey(GenerateQL.PARAM_TOP)) {
389 Object top = queryParams.get(GenerateQL.PARAM_TOP);
390 int topValue = 0;
391 if (top instanceof Integer) {
392 topValue = ((Integer) top).intValue();
393 }else if (top instanceof String && !"".equals((String)top) &&((String) top).trim().matches("[0-9]+")) {
394 topValue = Integer.parseInt((String)top);
395 }else {
396 throw new RuntimeException(tableClass.getName() + ",top value only support by query as a Integer or String type.");
397 }
398 query.setFirstResult(0);
399 query.setMaxResults(topValue);
400 }
401 result = query.list();
402 } else if (tableClass.isAnnotationPresent(Relations.class)) {
403 result = new LinkedList<Object>();
404 hql = qp.generateSQL(queryParams, getDialect());
405 query = createSQLQuery(hql, convertParamsForVO(queryParams));
406 result = convertResultToVO(tableClass, query.list());
407 } else {
408 throw new RuntimeException(tableClass.getName() + " isn't support by query as domain class.");
409 }
410 return result;
411 }
412
413
414
415
416 @Override
417 public List<?> queryList(String[][] queryParams) {
418 Map<String, Object> map = new HashMap<String, Object>();
419 String[] item;
420
421 for (int i = 0; i < queryParams.length; i++) {
422 item = queryParams[i];
423 if (item.length == 1) {
424 map.put(QuerySupport.PARAM_TABLENAME, item[0]);
425 } else if (item.length == 2) {
426 map.put(item[0], item[1]);
427 } else {
428 throw new RuntimeException("the parameter for getPage(String[][] params) is invalid.");
429 }
430 }
431
432 return queryList(map);
433 }
434
435
436
437
438 @Override
439 public List<?> queryListByHql(String hsql, Map<String, Object> params) {
440 Query q = createHQLQuery(hsql, params);
441 return q.list();
442 }
443
444
445
446
447 @Override
448 public List<?> queryListBySql(String sql, Map<String, Object> params) {
449 Query query = createSQLQuery(sql, params);
450 return query.list();
451 }
452
453
454
455
456 @Override
457 public String save(Object object) {
458 String ls = (String) getHibernateTemplate().save(object);
459 return ls;
460 }
461
462
463
464
465 @Override
466 public void update(Object object) {
467 getHibernateTemplate().update(object);
468 }
469
470
471
472
473 @Override
474 public void updateByQuery(String queryString, Map<String, Object> params) {
475 Query query = createHQLQuery(queryString, params);
476 query.executeUpdate();
477 }
478
479 private Query createSQLQuery(final String sql, final Map<String, Object> params) {
480 return getHibernateTemplate().execute(new HibernateCallback<Query>() {
481 public Query doInHibernate(Session session) {
482 Query query = session.createSQLQuery(sql);
483 String[] keys = query.getNamedParameters();
484
485 if (keys != null && params != null) {
486 for (String key : keys) {
487 if (!params.containsKey(key)) {
488 throw new RuntimeException("没有设置参数" + key + "的值");
489 }
490 Object value = params.get(key);
491 if (value instanceof String[]) {
492 String[] getValue = (String[]) value;
493 if (getValue.length > 0) {
494 value = getValue[0];
495 } else {
496 value = "";
497 }
498 }if(value instanceof Date){
499 query.setParameter(key, value,Hibernate.DATE);
500 continue;
501 }
502 query.setParameter(key, value);
503 }
504 }
505 return query;
506 }
507 });
508 }
509
510 private Query createHQLQuery(final String hsql, final Map<String, Object> params) {
511 return getHibernateTemplate().execute(new HibernateCallback<Query>() {
512 public Query doInHibernate(Session session) {
513 Query query = session.createQuery(hsql);
514 String[] keys = query.getNamedParameters();
515
516 if (keys != null && params != null) {
517 for (String key : keys) {
518 if (!params.containsKey(key)) {
519 throw new RuntimeException("没有设置参数" + key + "的值");
520 }
521 Object value = params.get(key);
522 if (value instanceof String[]) {
523 String[] getValue = (String[]) value;
524 if (getValue.length > 0) {
525 value = getValue[0];
526 } else {
527 value = "";
528 }
529 }
530 query.setParameter(key, value);
531 }
532 }
533 return query;
534 }
535 });
536 }
537
538 private int getCountNumber(String hsql, Map<String, Object> params) {
539 Query query = createHQLQuery(hsql, params);
540 ScrollableResults scrollableResults = query.scroll(ScrollMode.SCROLL_SENSITIVE);
541 scrollableResults.last();
542
543 int totalCount = scrollableResults.getRowNumber() + 1;
544 return totalCount;
545 }
546
547 private Dialect getDialect() {
548 return ((SessionImpl) getSession()).getFactory().getDialect();
549 }
550
551 private List<Object> convertResultToVO(Class<?> tableClass, List<?> result) {
552 Object[] item;
553 List<Object> converted = new LinkedList<Object>();
554 Object domain;
555 int index = 0;
556 for (int i = 0; i < result.size(); i++) {
557 item = (Object[]) result.get(i);
558 try {
559 domain = tableClass.newInstance();
560
561 List<Field> fields = new LinkedList<Field>();
562 Field[] ownFields = tableClass.getDeclaredFields();
563 gboat2.base.core.annotation.Field fieldAnnotation;
564 List<String> voFieldNames = new LinkedList<String>();
565 for (Field f : ownFields) {
566 fieldAnnotation = f.getAnnotation(gboat2.base.core.annotation.Field.class);
567 if (fieldAnnotation != null && fieldAnnotation.query()) {
568 fields.add(f);
569 }
570 voFieldNames.add(f.getName());
571 }
572 if (!tableClass.getSuperclass().getSimpleName().equals("Object")) {
573 Field[] superFields = tableClass.getSuperclass().getDeclaredFields();
574 for (Field f : superFields) {
575 if (!voFieldNames.contains(f.getName())) {
576 fields.add(f);
577 }
578 }
579 }
580 index = 0;
581 Object value;
582 for (Field field : fields) {
583 if (index < item.length) {
584 field.setAccessible(true);
585 value = getValue(field, item[index++]);
586 if (value != null) {
587
588 try {
589 field.set(domain, value);
590 } catch (IllegalArgumentException e) {
591 logger.error("failed to deal " + field.getName(), e);
592 }
593 }
594 }
595 }
596 } catch (InstantiationException e) {
597 throw new RuntimeException("can't instance class : " + tableClass.getName() + ". Error : " + e.getMessage());
598 } catch (IllegalAccessException e) {
599 throw new RuntimeException("can't instance class : " + tableClass.getName() + ". Error : " + e.getMessage());
600 } catch (SecurityException e) {
601 throw new RuntimeException("can't set value to " + tableClass.getName());
602 }
603 converted.add(domain);
604 }
605 return converted;
606 }
607
608 @SuppressWarnings("rawtypes")
609 private Object getValue(Field field, Object value) {
610 Object returnedValue = value;
611 if (value == null || field.getType() == value.getClass()) {
612 if (field.getType() == String.class) {
613 returnedValue = value == null ? "" : String.valueOf(value);
614 }
615 } else {
616
617 if (field.getType() == String.class) {
618 returnedValue = value.toString();
619 } else if (field.getType() == java.util.Date.class
620 && (value.getClass() == java.sql.Date.class || value.getClass() == java.sql.Time.class || value.getClass() == java.sql.Timestamp.class)) {
621
622 returnedValue = (java.util.Date) value;
623 } else if (field.getType() == java.sql.Date.class && value.getClass() == java.util.Date.class) {
624
625 returnedValue = new java.sql.Date(((java.util.Date) value).getTime());
626 } else if (field.getType().getSuperclass() == Enum.class) {
627 Enum[] enums = (Enum[]) field.getType().getEnumConstants();
628 for (Enum e : enums) {
629 if (e.name().equals(value) || value.equals(e.ordinal())) {
630 returnedValue = e;
631 break;
632 }
633 }
634 } else if (value.getClass() == java.math.BigDecimal.class) {
635 BigDecimal realValue = (BigDecimal) value;
636 if (field.getType() == java.lang.Integer.class) {
637
638 returnedValue = realValue.intValue();
639 } else if (field.getType() == java.lang.Double.class || field.getType() == double.class) {
640
641 returnedValue = realValue.doubleValue();
642 } else if (field.getType() == java.lang.Float.class || field.getType() == float.class) {
643
644 returnedValue = realValue.floatValue();
645 } else if (field.getType() == java.lang.Long.class || field.getType() == long.class) {
646
647 returnedValue = realValue.longValue();
648 } else {
649 throw new RuntimeException("type of Field[" + field.getName() + "] in VO is " + field.getType() + ", but in PO it's "
650 + value.getClass());
651 }
652 } else if (value.getClass() == Double.class) {
653 if (field.getType() == double.class) {
654 returnedValue = ((Double) value).doubleValue();
655 } else if (field.getType() == float.class || field.getType() == Float.class) {
656 returnedValue = ((Double) value).floatValue();
657 }
658 } else if (value.getClass() == Float.class) {
659 if (field.getType() == double.class || field.getType() == Double.class) {
660 returnedValue = ((Double) value).doubleValue();
661 } else if (field.getType() == float.class) {
662 returnedValue = ((Float) value).floatValue();
663 }
664 } else if (value.getClass() == Integer.class) {
665 if (field.getType() == int.class) {
666 returnedValue = ((Integer) value).intValue();
667 }
668 if (field.getType() == boolean.class || field.getType() == Boolean.class) {
669 returnedValue = ((Integer) value).intValue();
670 if(((Integer) value).intValue() == 1){
671 returnedValue = Boolean.TRUE;
672 }else{
673 return Boolean.FALSE;
674 }
675 }
676 } else if (value.getClass() == BigInteger.class) {
677 if (field.getType() == java.lang.Long.class || field.getType() == long.class) {
678
679 returnedValue = Long.valueOf(value.toString());
680 } if(field.getType() == java.lang.Integer.class) {
681
682 returnedValue = Integer.valueOf(value.toString());
683 } else if (field.getType() == java.lang.Double.class || field.getType() == double.class) {
684
685 returnedValue = Double.valueOf(value.toString());
686 } else if (field.getType() == java.lang.Float.class || field.getType() == float.class) {
687
688 returnedValue = Float.valueOf(value.toString());
689 }
690 } else {
691 throw new RuntimeException("type of Field[" + field.getName() + "] in VO is " + field.getType() + ", but in PO it's "
692 + value.getClass());
693 }
694 }
695 return returnedValue;
696 }
697
698 protected void loadClass(String clazz) {
699 Bundle[] bundles = bundleContext.getBundles();
700 for (Bundle bundle : bundles) {
701 URL url = bundle.getResource(clazz);
702 System.out.println(url);
703 }
704 }
705
706 @Override
707 public void setBundleContext(BundleContext bundleContext) {
708 this.bundleContext = bundleContext;
709 }
710
711
712
713
714 @Override
715 public void evict(Object object) {
716 ((SessionImpl) getSession()).evict(object);
717 }
718
719
720
721
722 @Override
723 public void saveOrUpdate(Object object) {
724 getHibernateTemplate().saveOrUpdate(object);
725 }
726
727 @Override
728 public int executeUpdateSql(String sql,final Map<String, Object> params){
729 if(sql.trim().toLowerCase().startsWith("select")){
730 throw new RuntimeException("'select' is not support in method executesql");
731 }
732 Query query = createSQLQuery(sql, params);
733 return query.executeUpdate();
734 }
735
736 @Override
737 public Page<?> getPageByHql(String hql, Map<String, Object> params) {
738
739 Object setedPage = params.get(QuerySupport.PARAM_PAGE);
740 int page = 1;
741 if (setedPage instanceof String) {
742 try {
743 page = Integer.parseInt((String) setedPage);
744 } catch (NumberFormatException e) {
745 page = 1;
746 }
747 } else if (setedPage instanceof String[]) {
748 if (((String[]) setedPage).length > 0) {
749 try {
750 page = Integer.parseInt(((String[]) setedPage)[0]);
751 } catch (NumberFormatException e) {
752 page = 1;
753 }
754 }
755 } else if (setedPage instanceof Integer) {
756 page = (Integer) setedPage;
757 }
758
759 Object pageSizeParam = params.get(QuerySupport.PARAM_PAGESIZE);
760 int pagesize = QuerySupport.PAGESIZE_DEFAULT;
761 if (pageSizeParam instanceof String) {
762 try {
763 pagesize = Integer.parseInt((String) pageSizeParam);
764 } catch (NumberFormatException e) {
765 pagesize = QuerySupport.PAGESIZE_DEFAULT;
766 }
767 } else if (pageSizeParam instanceof String[]) {
768 String[] pageSizes = (String[]) pageSizeParam;
769 if (pageSizes.length > 0) {
770 try {
771 pagesize = Integer.parseInt(pageSizes[0]);
772 } catch (NumberFormatException e) {
773 e.printStackTrace();
774 }
775 }
776 } else if (pageSizeParam instanceof Integer) {
777 pagesize = (Integer) pageSizeParam;
778 }
779
780
781 if(pageSizeParam == null){
782 IParameterService service = (IParameterService)bundleContext.getService(bundleContext.getServiceReference(IParameterService.class.getName()));
783 String jsonOfParameter = "{'desc':'设置全局分页参数','groupName':'G2平台参数','code':'CODE_PAGESIZE','name':'全局分页数据','value':'20'}";
784 Parameter initOrGetPlatformParameter = service.getParameter(jsonOfParameter);
785 String parameterValue = initOrGetPlatformParameter.getValue();
786 try {
787 Integer platform_pagesize = Integer.valueOf(parameterValue);
788 pagesize = platform_pagesize;
789 } catch (Exception e) {
790 logger.info("platform_pagesize is invalid, if you do not need this parameter , ignore this ;other,check configration and change value to right");
791 }
792 }
793
794 int sizeForPageBean = pagesize;
795 if (pagesize == -1) {
796 sizeForPageBean = Integer.MAX_VALUE;
797 }
798 PageBean pageBean = new PageBean(page, sizeForPageBean);
799
800 Query query = this.createHQLQuery(hql, params);
801 ScrollableResults scrollableResults = query.scroll(ScrollMode.SCROLL_SENSITIVE);
802 scrollableResults.last();
803
804 int cnt = 0;
805 cnt = scrollableResults.getRowNumber() + 1;
806
807 pageBean.setCount(cnt);
808 pageBean.setCurrentPageParams(GenerateQL.getQueryParamsAsUrlString(params));
809 if (pageBean.getCount() == 0) {
810 return new Page<Object>(new ArrayList<Object>(), pageBean);
811 }
812 if (pagesize != -1) {
813 query.setMaxResults(pagesize);
814 }
815 query.setFirstResult(pageBean.getStartNo() - 1);
816 @SuppressWarnings("unchecked")
817 List<Object> result = query.list();
818
819 return new Page<Object>(result, pageBean);
820 }
821
822 }