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 | |||
899 | dev | 77 | try { |
78 | HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination); |
||
79 | } |
||
80 | catch(HibernateException ex) |
||
81 | { |
||
82 | throw new ModelException(ex); |
||
83 | } |
||
84 | } |
||
85 | |||
915 | dev | 86 | public void delete(User editor, MailAliasDestination mailAliasDestination) |
899 | dev | 87 | throws ModelException |
88 | { |
||
915 | dev | 89 | if(!mailAliasDestination.deleteableBy(editor)) |
90 | throw new ModelSecurityException(); |
||
91 | |||
899 | dev | 92 | try { |
93 | HibernateUtil.currentSession().delete(mailAliasDestination); |
||
94 | } |
||
95 | catch(HibernateException ex) |
||
96 | { |
||
97 | throw new ModelException(ex); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public Collection listMailAliasesDestination(MailAlias alias) |
||
102 | throws ModelException |
||
103 | { |
||
104 | try { |
||
105 | return HibernateUtil.currentSession().find( |
||
106 | "from MailAliasDestination where alias=?", |
||
107 | alias, Hibernate.entity(MailAlias.class)); |
||
108 | } |
||
109 | catch(HibernateException ex) |
||
110 | { |
||
111 | throw new ModelException(ex); |
||
112 | } |
||
113 | } |
||
114 | |||
915 | dev | 115 | public boolean areMailAliasesDestinationsAvailable(User editor) |
116 | throws ModelException |
||
117 | { |
||
118 | return true; |
||
119 | } |
||
120 | |||
899 | dev | 121 | private static MailAliasDestinationManager mailAliasDestinationManager = null; |
122 | |||
123 | public static MailAliasDestinationManager getInstance() |
||
124 | { |
||
125 | if(mailAliasDestinationManager == null) |
||
126 | mailAliasDestinationManager = new MailAliasDestinationManager(); |
||
127 | |||
128 | return mailAliasDestinationManager; |
||
129 | } |
||
130 | |||
131 | public static final Comparator EMAIL_COMPARATOR = new EmailComparator(); |
||
132 | |||
133 | private static class EmailComparator |
||
134 | implements Comparator |
||
135 | { |
||
136 | public int compare(Object o1, Object o2) |
||
137 | { |
||
138 | if(!(o1 instanceof MailAliasDestination) || !(o2 instanceof MailAliasDestination)) |
||
139 | throw new ClassCastException("not a MailAliasDestination"); |
||
140 | |||
141 | MailAliasDestination a1 = (MailAliasDestination)o1; |
||
142 | MailAliasDestination a2 = (MailAliasDestination)o2; |
||
143 | |||
144 | if(a1 == null && a2 == null) |
||
145 | return 0; |
||
146 | else if(a1 == null && a2 != null) |
||
147 | return -1; |
||
148 | else if(a1 != null && a2 == null) |
||
149 | return 1; |
||
150 | else |
||
151 | return a1.getEmail().compareToIgnoreCase(a2.getEmail()); |
||
152 | } |
||
153 | |||
154 | public boolean equals(Object obj) |
||
155 | { |
||
156 | return (obj instanceof EmailComparator); |
||
157 | } |
||
158 | } |
||
159 | } |