/
ReportingUserCrud.java
120 lines (102 loc) · 3.81 KB
/
ReportingUserCrud.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*************************************************************************
* Copyright 2009-2012 Eucalyptus Systems, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
************************************************************************/
package com.eucalyptus.reporting.domain;
import java.util.*;
import org.apache.log4j.Logger;
import com.eucalyptus.entities.EntityWrapper;
/**
* <p>ReportingUserCrud is an object for CReating, Updating, and Deleting users. This class should
* be used instead of creating User hibernate objects and storing them directly.
*/
public class ReportingUserCrud
{
private static Logger LOG = Logger.getLogger( ReportingUserCrud.class );
private static ReportingUserCrud instance = null;
public static synchronized ReportingUserCrud getInstance()
{
if (instance == null) {
instance = new ReportingUserCrud();
}
return instance;
}
protected ReportingUserCrud()
{
}
/**
* Create or update a user. This method can be called repeatedly every time an event is
* received that has user data. This method will update the user with a new name if
* the name has changed, or will do nothing if the user already exists with the supplied
* name.
*/
public void createOrUpdateUser(String id, String accountId, String name)
{
if (id==null || accountId==null || name==null) throw new IllegalArgumentException("args cant be null");
if (ReportingAccountDao.getInstance().getReportingAccount(accountId)==null) {
LOG.error("Non-matching account for user, userId:" + id + " accountId:" + accountId + " name:" + name, new IllegalArgumentException());
}
ReportingUser user = new ReportingUser(id, accountId, name);
ReportingUser oldUser = ReportingUserDao.getInstance().getReportingUser(id);
if (oldUser!=null && oldUser.getName().equals(name)) {
return;
} else if (oldUser!=null) {
updateInDb(id, name);
ReportingUserDao.getInstance().putCache(user);
} else {
try {
addToDb(id, accountId, name);
ReportingUserDao.getInstance().putCache(user);
} catch (RuntimeException e) {
LOG.error(e);
}
}
}
private void updateInDb(String id, String name)
{
LOG.debug("Update reporting user in db, id:" + id + " name:" + name);
EntityWrapper<ReportingUser> entityWrapper =
EntityWrapper.get(ReportingUser.class);
try {
ReportingUser reportingUser = (ReportingUser)
entityWrapper.createQuery("from ReportingUser where id = ?")
.setString(0, id)
.uniqueResult();
reportingUser.setName(name);
entityWrapper.commit();
} catch (Exception ex) {
LOG.error(ex);
entityWrapper.rollback();
throw new RuntimeException(ex);
}
}
private void addToDb(String id, String accountId, String name)
{
LOG.debug("Add reporting user to db, id:" + id + " accountId:" + accountId + " name:" + name);
EntityWrapper<ReportingUser> entityWrapper =
EntityWrapper.get(ReportingUser.class);
try {
entityWrapper.add(new ReportingUser(id, accountId, name));
entityWrapper.commit();
} catch (Exception ex) {
LOG.error(ex);
entityWrapper.rollback();
throw new RuntimeException(ex);
}
}
}