Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247
WebSVN – general – Blame – /hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxManager.java/ – Rev 925

Subversion Repositories general

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
924 dev 1
package ak.hostadmiral.core.model;
919 dev 2
 
3
import java.util.*;
4
import net.sf.hibernate.*;
5
import net.sf.hibernate.type.Type;
924 dev 6
import ak.hostadmiral.util.HibernateUtil;
7
import ak.hostadmiral.util.ModelException;
8
import ak.hostadmiral.util.ModelSecurityException;
919 dev 9
 
10
public class MailboxManager
11
{
12
	private static boolean registered = false;
13
	protected static void register()
14
	{
15
		synchronized(MailboxManager.class) {
16
			if(registered) return;
17
 
18
			registered = true;
19
			try {
20
				HibernateUtil.getConfiguration().addResource(
924 dev 21
					"/ak/hostadmiral/core/model/Mailbox.hbm.xml");
919 dev 22
			}
23
			catch(Exception ex) {
24
				ex.printStackTrace();
25
				throw new RuntimeException(ex.getMessage());
26
			}
27
		}
28
	}
29
 
30
	static {
31
		register();
32
	}
33
 
34
	private MailboxManager()
35
	{
36
	}
37
 
38
	public Mailbox create(User editor)
39
		throws ModelException
40
	{
41
		if(!allowedToCreate(editor)) throw new ModelSecurityException();
42
 
43
		return new Mailbox();
44
	}
45
 
46
	public boolean allowedToCreate(User editor)
47
		throws ModelException
48
	{
921 dev 49
		return Mailbox.allowedToCreate(this, editor);
919 dev 50
	}
51
 
52
	public Mailbox get(User editor, Long id)
53
		throws ModelException
54
	{
55
		Mailbox mailbox;
56
 
57
		try {
58
			mailbox = (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id);
59
		}
60
		catch(HibernateException ex)
61
		{
62
			throw new ModelException(ex);
63
		}
64
 
65
		if(!mailbox.viewableBy(editor))
66
			throw new ModelSecurityException();
67
 
68
		return mailbox;
69
	}
70
 
71
	protected Mailbox findForLogin(String login)
72
		throws ModelException
73
	{
74
		try {
75
			List list = HibernateUtil.currentSession().find(
76
				"from Mailbox where login=?", login, Hibernate.STRING);
77
 
78
			if(list.size() == 0)
79
				return null;
80
			else
81
				return (Mailbox)list.get(0);
82
		}
83
		catch(HibernateException ex)
84
		{
85
			throw new ModelException(ex);
86
		}
87
	}
88
 
89
	public void save(User editor, Mailbox mailbox)
90
		throws ModelException
91
	{
92
		if(!mailbox.editableBy(editor))
93
			throw new ModelSecurityException();
94
 
95
		mailbox.setModUser(editor);
96
 
97
		try {
98
			HibernateUtil.currentSession().saveOrUpdate(mailbox);
99
		}
100
		catch(HibernateException ex)
101
		{
102
			throw new ModelException(ex);
103
		}
104
	}
105
 
106
	public void delete(User editor, Mailbox mailbox)
107
		throws ModelException
108
	{
109
		if(!mailbox.deleteableBy(editor))
110
			throw new ModelSecurityException();
111
 
112
		try {
113
			HibernateUtil.currentSession().delete(mailbox);
114
		}
115
		catch(HibernateException ex)
116
		{
117
			throw new ModelException(ex);
118
		}
119
	}
120
 
121
	public Collection listMailboxes(User editor)
122
		throws ModelException
123
	{
124
		try {
125
			if(editor.isSuperuser())
126
				return HibernateUtil.currentSession().find("from Mailbox");
127
			else
128
				return HibernateUtil.currentSession().find(
129
					"select mb from Mailbox mb left join mb.domain as d"
130
					+ " where d.owner=? or mb.owner=?",
131
					new Object[] { editor, editor },
132
					new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
133
		}
134
		catch(HibernateException ex)
135
		{
136
			throw new ModelException(ex);
137
		}
138
	}
139
 
140
	public boolean areMailboxesAvailable(User editor)
141
		throws ModelException
142
	{
143
		try {
144
			if(editor.isSuperuser()
145
				|| InetDomainManager.getInstance().areInetDomainsAvailable(editor))
146
			{
147
				return true;
148
			}
149
			else {
150
				return ((Integer)HibernateUtil.currentSession().iterate(
151
					"select count(*) from Mailbox mb left join mb.domain as d"
152
					+ " where d.owner=? or mb.owner=?",
153
					new Object[] { editor, editor },
154
					new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) })
155
					.next()).intValue() > 0;
156
			}
157
		}
158
		catch(HibernateException ex)
159
		{
160
			throw new ModelException(ex);
161
		}
162
	}
163
 
164
	private static MailboxManager mailboxManager = null;
165
 
166
	public static MailboxManager getInstance()
167
	{
168
		if(mailboxManager == null)
169
			mailboxManager = new MailboxManager();
170
 
171
		return mailboxManager;
172
	}
173
 
174
	public static final Comparator LOGIN_COMPARATOR = new LoginComparator();
175
 
176
	private static class LoginComparator
177
		implements Comparator
178
	{
179
		public int compare(Object o1, Object o2)
180
		{
181
			if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox))
182
				throw new ClassCastException("not a Mailbox");
183
 
184
		    Mailbox a1 = (Mailbox)o1;
185
		    Mailbox a2 = (Mailbox)o2;
186
 
187
		    if(a1 == null && a2 == null)
188
		    	return 0;
189
		    else if(a1 == null && a2 != null)
190
		    	return -1;
191
		    else if(a1 != null && a2 == null)
192
		    	return 1;
193
		    else
194
		    	return a1.getLogin().compareToIgnoreCase(a2.getLogin());
195
		}
196
 
197
		public boolean equals(Object obj)
198
		{
199
			return (obj instanceof LoginComparator);
200
		}
201
	}
202
}