]> git.argeo.org Git - lgpl/argeo-commons.git/blob - HibernateLightDaoSync.java
365565fcd47aa19d63233265c723f68e971c0a7b
[lgpl/argeo-commons.git] / HibernateLightDaoSync.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.server.hibernate;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
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;
26
27 public class HibernateLightDaoSync {
28 private final static Log log = LogFactory
29 .getLog(HibernateLightDaoSync.class);
30
31 private String externalSuffix = LightDaoInterceptor.DEFAULT_EXTERNAL_SUFFIX;
32
33 private SessionFactory sessionFactory;
34
35 private LightDaoSupport lightDaoSupport;
36
37 private List<Class<?>> classes = new ArrayList<Class<?>>();
38
39 public void sync() {
40 List<Class<?>> lst;
41 if (classes.size() > 0)
42 lst = classes;
43 else
44 lst = lightDaoSupport.getSupportedClasses();
45
46 Session session = sessionFactory.getCurrentSession();
47 session.beginTransaction();
48 try {
49 for (Class<?> clss : lst) {
50 String entityName = clss.getSimpleName() + externalSuffix;
51 int count = 0;
52 for (Object obj : lightDaoSupport.list(clss, null)) {
53 session.save(entityName, obj);
54 count++;
55 }
56 if (log.isDebugEnabled())
57 log.debug("Synchronized " + count + "\tentities '"
58 + entityName + "'");
59 }
60 session.getTransaction().commit();
61 } catch (Exception e) {
62 session.getTransaction().rollback();
63 }
64 }
65
66 public void setClasses(List<Class<?>> classes) {
67 this.classes = classes;
68 }
69
70 public void setExternalSuffix(String externalSuffix) {
71 this.externalSuffix = externalSuffix;
72 }
73
74 public void setSessionFactory(SessionFactory sessionFactory) {
75 this.sessionFactory = sessionFactory;
76 }
77
78 public void setLightDaoSupport(LightDaoSupport lightDaoSupport) {
79 this.lightDaoSupport = lightDaoSupport;
80 }
81 }