2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.server
.hibernate
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.server
.dao
.LightDaoSupport
;
24 import org
.hibernate
.Session
;
25 import org
.hibernate
.SessionFactory
;
27 public class HibernateLightDaoSync
{
28 private final static Log log
= LogFactory
29 .getLog(HibernateLightDaoSync
.class);
31 private String externalSuffix
= LightDaoInterceptor
.DEFAULT_EXTERNAL_SUFFIX
;
33 private SessionFactory sessionFactory
;
35 private LightDaoSupport lightDaoSupport
;
37 private List
<Class
<?
>> classes
= new ArrayList
<Class
<?
>>();
41 if (classes
.size() > 0)
44 lst
= lightDaoSupport
.getSupportedClasses();
46 Session session
= sessionFactory
.getCurrentSession();
47 session
.beginTransaction();
49 for (Class
<?
> clss
: lst
) {
50 String entityName
= clss
.getSimpleName() + externalSuffix
;
52 for (Object obj
: lightDaoSupport
.list(clss
, null)) {
53 session
.save(entityName
, obj
);
56 if (log
.isDebugEnabled())
57 log
.debug("Synchronized " + count
+ "\tentities '"
60 session
.getTransaction().commit();
61 } catch (Exception e
) {
62 session
.getTransaction().rollback();
66 public void setClasses(List
<Class
<?
>> classes
) {
67 this.classes
= classes
;
70 public void setExternalSuffix(String externalSuffix
) {
71 this.externalSuffix
= externalSuffix
;
74 public void setSessionFactory(SessionFactory sessionFactory
) {
75 this.sessionFactory
= sessionFactory
;
78 public void setLightDaoSupport(LightDaoSupport lightDaoSupport
) {
79 this.lightDaoSupport
= lightDaoSupport
;