Subversion Repositories general

Rev

Rev 1011 | Rev 1041 | Go to most recent revision | Details | Compare with Previous | 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.*;
924 dev 5
import ak.hostadmiral.util.HibernateUtil;
6
import ak.hostadmiral.util.ModelException;
7
import ak.hostadmiral.util.ModelSecurityException;
919 dev 8
 
9
public class MailAliasDestinationManager
10
{
1011 dev 11
	// FIXME create, delete and modify listeners are not implemented, bacause
12
	//       all operations are done via MailAliasManager. Do we need them?
13
 
919 dev 14
	private static boolean registered = false;
15
	protected static void register()
16
	{
17
		synchronized(MailAliasDestinationManager.class) {
18
			if(registered) return;
19
 
20
			registered = true;
21
			try {
22
				HibernateUtil.getConfiguration().addResource(
950 dev 23
					"ak/hostadmiral/core/model/MailAliasDestination.hbm.xml");
919 dev 24
			}
25
			catch(Exception ex) {
26
				ex.printStackTrace();
27
				throw new RuntimeException(ex.getMessage());
28
			}
29
		}
30
	}
31
 
32
	static {
33
		register();
34
	}
35
 
36
	private MailAliasDestinationManager()
37
	{
38
	}
39
 
40
	public MailAliasDestination create(User editor)
41
		throws ModelException
42
	{
43
		if(!allowedToCreate(editor)) throw new ModelSecurityException();
44
 
45
		return new MailAliasDestination();
46
	}
47
 
48
	public boolean allowedToCreate(User editor)
49
		throws ModelException
50
	{
922 dev 51
		return MailAliasDestination.allowedToCreate(this, editor);
919 dev 52
	}
53
 
54
	public MailAliasDestination get(User editor, Long id)
55
		throws ModelException
56
	{
57
		MailAliasDestination dest;
58
 
59
		try {
60
			dest = (MailAliasDestination)HibernateUtil.currentSession()
61
				.load(MailAliasDestination.class, id);
62
		}
63
		catch(HibernateException ex)
64
		{
65
			throw new ModelException(ex);
66
		}
67
 
68
		if(!dest.viewableBy(editor))
69
			throw new ModelSecurityException();
70
 
71
		return dest;
72
	}
73
 
74
	public void save(User editor, MailAliasDestination mailAliasDestination)
75
		throws ModelException
76
	{
77
		if(!mailAliasDestination.editableBy(editor))
78
			throw new ModelSecurityException();
79
 
1010 dev 80
		//mailAliasDestination.setModUser(editor); // FIXME
923 dev 81
        // FIXME: the mod_user is not set when changing a destination as element of collection
919 dev 82
 
83
		try {
84
			HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination);
85
		}
86
		catch(HibernateException ex)
87
		{
88
			throw new ModelException(ex);
89
		}
90
	}
91
 
92
	public void delete(User editor, MailAliasDestination mailAliasDestination)
93
		throws ModelException
94
	{
95
		if(!mailAliasDestination.deleteableBy(editor))
96
			throw new ModelSecurityException();
97
 
98
		try {
99
			HibernateUtil.currentSession().delete(mailAliasDestination);
100
		}
101
		catch(HibernateException ex)
102
		{
103
			throw new ModelException(ex);
104
		}
105
	}
106
 
107
	public Collection listMailAliasesDestination(MailAlias alias)
108
		throws ModelException
109
	{
110
		try {
111
			return HibernateUtil.currentSession().find(
1018 dev 112
				"select d from MailAliasDestination d left join fetch d.mailbox where d.alias=?",
919 dev 113
				alias, Hibernate.entity(MailAlias.class));
114
		}
115
		catch(HibernateException ex)
116
		{
117
			throw new ModelException(ex);
118
		}
119
	}
120
 
121
	public boolean areMailAliasesDestinationsAvailable(User editor)
122
		throws ModelException
123
	{
124
		return true;
125
	}
126
 
127
	private static MailAliasDestinationManager mailAliasDestinationManager = null;
128
 
129
	public static MailAliasDestinationManager getInstance()
130
	{
131
		if(mailAliasDestinationManager == null)
132
			mailAliasDestinationManager = new MailAliasDestinationManager();
133
 
134
		return mailAliasDestinationManager;
135
	}
136
 
137
	public static final Comparator EMAIL_COMPARATOR = new EmailComparator();
138
 
139
	private static class EmailComparator
140
		implements Comparator
141
	{
142
		public int compare(Object o1, Object o2)
143
		{
144
			if(!(o1 instanceof MailAliasDestination) || !(o2 instanceof MailAliasDestination))
145
				throw new ClassCastException("not a MailAliasDestination");
146
 
147
		    MailAliasDestination a1 = (MailAliasDestination)o1;
148
		    MailAliasDestination a2 = (MailAliasDestination)o2;
149
 
150
		    if(a1 == null && a2 == null)
151
		    	return 0;
152
		    else if(a1 == null && a2 != null)
153
		    	return -1;
154
		    else if(a1 != null && a2 == null)
155
		    	return 1;
156
		    else
157
		    	return a1.getEmail().compareToIgnoreCase(a2.getEmail());
158
		}
159
 
160
		public boolean equals(Object obj)
161
		{
162
			return (obj instanceof EmailComparator);
163
		}
164
	}
165
}