Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
904 | dev | 1 | package ak.hostcaptain.core.model; |
899 | dev | 2 | |
3 | import java.util.*; |
||
4 | import net.sf.hibernate.*; |
||
904 | dev | 5 | import ak.hostcaptain.util.HibernateUtil; |
6 | import ak.hostcaptain.util.ModelException; |
||
915 | dev | 7 | import ak.hostcaptain.util.ModelSecurityException; |
899 | dev | 8 | |
9 | public class MailAliasDestinationManager |
||
10 | { |
||
11 | private static boolean registered = false; |
||
12 | protected static void register() |
||
13 | { |
||
14 | synchronized(MailAliasDestinationManager.class) { |
||
15 | if(registered) return; |
||
16 | |||
17 | registered = true; |
||
18 | try { |
||
19 | HibernateUtil.getConfiguration().addResource( |
||
904 | dev | 20 | "/ak/hostcaptain/core/model/MailAliasDestination.hbm.xml"); |
899 | dev | 21 | } |
22 | catch(Exception ex) { |
||
23 | ex.printStackTrace(); |
||
24 | throw new RuntimeException(ex.getMessage()); |
||
25 | } |
||
26 | } |
||
27 | } |
||
28 | |||
29 | static { |
||
30 | register(); |
||
31 | } |
||
32 | |||
33 | private MailAliasDestinationManager() |
||
34 | { |
||
35 | } |
||
36 | |||
915 | dev | 37 | public MailAliasDestination create(User editor) |
38 | throws ModelException |
||
899 | dev | 39 | { |
915 | dev | 40 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
41 | |||
899 | dev | 42 | return new MailAliasDestination(); |
43 | } |
||
44 | |||
915 | dev | 45 | public boolean allowedToCreate(User editor) |
899 | dev | 46 | throws ModelException |
47 | { |
||
915 | dev | 48 | return true; |
49 | } |
||
50 | |||
51 | public MailAliasDestination get(User editor, Long id) |
||
52 | throws ModelException |
||
53 | { |
||
54 | MailAliasDestination dest; |
||
55 | |||
899 | dev | 56 | try { |
915 | dev | 57 | dest = (MailAliasDestination)HibernateUtil.currentSession() |
899 | dev | 58 | .load(MailAliasDestination.class, id); |
59 | } |
||
60 | catch(HibernateException ex) |
||
61 | { |
||
62 | throw new ModelException(ex); |
||
63 | } |
||
915 | dev | 64 | |
65 | if(!dest.viewableBy(editor)) |
||
66 | throw new ModelSecurityException(); |
||
67 | |||
68 | return dest; |
||
899 | dev | 69 | } |
70 | |||
915 | dev | 71 | public void save(User editor, MailAliasDestination mailAliasDestination) |
899 | dev | 72 | throws ModelException |
73 | { |
||
915 | dev | 74 | if(!mailAliasDestination.editableBy(editor)) |
75 | throw new ModelSecurityException(); |
||
76 | |||
918 | dev | 77 | mailAliasDestination.setModUser(editor); |
78 | |||
899 | dev | 79 | try { |
80 | HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination); |
||
81 | } |
||
82 | catch(HibernateException ex) |
||
83 | { |
||
84 | throw new ModelException(ex); |
||
85 | } |
||
86 | } |
||
87 | |||
915 | dev | 88 | public void delete(User editor, MailAliasDestination mailAliasDestination) |
899 | dev | 89 | throws ModelException |
90 | { |
||
915 | dev | 91 | if(!mailAliasDestination.deleteableBy(editor)) |
92 | throw new ModelSecurityException(); |
||
93 | |||
899 | dev | 94 | try { |
95 | HibernateUtil.currentSession().delete(mailAliasDestination); |
||
96 | } |
||
97 | catch(HibernateException ex) |
||
98 | { |
||
99 | throw new ModelException(ex); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public Collection listMailAliasesDestination(MailAlias alias) |
||
104 | throws ModelException |
||
105 | { |
||
106 | try { |
||
107 | return HibernateUtil.currentSession().find( |
||
108 | "from MailAliasDestination where alias=?", |
||
109 | alias, Hibernate.entity(MailAlias.class)); |
||
110 | } |
||
111 | catch(HibernateException ex) |
||
112 | { |
||
113 | throw new ModelException(ex); |
||
114 | } |
||
115 | } |
||
116 | |||
915 | dev | 117 | public boolean areMailAliasesDestinationsAvailable(User editor) |
118 | throws ModelException |
||
119 | { |
||
120 | return true; |
||
121 | } |
||
122 | |||
899 | dev | 123 | private static MailAliasDestinationManager mailAliasDestinationManager = null; |
124 | |||
125 | public static MailAliasDestinationManager getInstance() |
||
126 | { |
||
127 | if(mailAliasDestinationManager == null) |
||
128 | mailAliasDestinationManager = new MailAliasDestinationManager(); |
||
129 | |||
130 | return mailAliasDestinationManager; |
||
131 | } |
||
132 | |||
133 | public static final Comparator EMAIL_COMPARATOR = new EmailComparator(); |
||
134 | |||
135 | private static class EmailComparator |
||
136 | implements Comparator |
||
137 | { |
||
138 | public int compare(Object o1, Object o2) |
||
139 | { |
||
140 | if(!(o1 instanceof MailAliasDestination) || !(o2 instanceof MailAliasDestination)) |
||
141 | throw new ClassCastException("not a MailAliasDestination"); |
||
142 | |||
143 | MailAliasDestination a1 = (MailAliasDestination)o1; |
||
144 | MailAliasDestination a2 = (MailAliasDestination)o2; |
||
145 | |||
146 | if(a1 == null && a2 == null) |
||
147 | return 0; |
||
148 | else if(a1 == null && a2 != null) |
||
149 | return -1; |
||
150 | else if(a1 != null && a2 == null) |
||
151 | return 1; |
||
152 | else |
||
153 | return a1.getEmail().compareToIgnoreCase(a2.getEmail()); |
||
154 | } |
||
155 | |||
156 | public boolean equals(Object obj) |
||
157 | { |
||
158 | return (obj instanceof EmailComparator); |
||
159 | } |
||
160 | } |
||
161 | } |