2
2
3
3
import main .exceptions .AqualityException ;
4
4
import main .exceptions .AqualityPermissionsException ;
5
- import main .model .db .dao .customer .CustomerAttachmentsDao ;
6
- import main .model .db .dao .customer .CustomerCommentsDao ;
7
5
import main .model .db .dao .customer .CustomerDao ;
8
- import main .model .db .dao .customer .CustomerMembersDao ;
9
6
import main .model .db .dao .project .ProjectDao ;
10
- import main .model .db .dao .project .ProjectUserDao ;
11
7
import main .model .db .dao .project .UserDao ;
12
8
import main .model .dto .*;
13
- import main .utils .FileUtils ;
14
9
15
10
import java .util .ArrayList ;
16
11
import java .util .List ;
17
12
18
13
public class CustomerController extends BaseController <CustomerDto > {
19
14
private CustomerDao customerDao ;
20
- private CustomerAttachmentsDao customerAttachmentsDao ;
21
- private CustomerCommentsDao customerCommentsDao ;
22
- private CustomerMembersDao customerMembersDao ;
23
15
private UserDao userDao ;
24
- private ProjectUserDao projectUserDao ;
25
16
26
17
public CustomerController (UserDto user ) {
27
18
super (user );
28
19
customerDao = new CustomerDao ();
29
- customerAttachmentsDao = new CustomerAttachmentsDao ();
30
- customerCommentsDao = new CustomerCommentsDao ();
31
- customerMembersDao = new CustomerMembersDao ();
32
- projectUserDao = new ProjectUserDao ();
33
20
userDao = new UserDao ();
34
21
}
35
22
@@ -47,68 +34,14 @@ public List<CustomerDto> get(CustomerDto template, boolean withChildren) throws
47
34
}
48
35
}
49
36
50
- public List <CustomerMemberDto > get (CustomerMemberDto template ) throws AqualityException {
51
- if (baseUser .isCoordinator ()){
52
- return customerMembersDao .searchAll (template );
53
- }else {
54
- throw new AqualityPermissionsException ("Account is not allowed to view Customer Members" , baseUser );
55
- }
56
- }
57
-
58
- public List <CustomerCommentDto > get (CustomerCommentDto template ) throws AqualityException {
59
- if (baseUser .isCoordinator ()){
60
- return completeComments (customerCommentsDao .searchAll (template ));
61
- }else {
62
- throw new AqualityPermissionsException ("Account is not allowed to view Customer Comments" , baseUser );
63
- }
64
- }
65
-
66
- public List <CustomerAttachmentDto > get (CustomerAttachmentDto template ) throws AqualityException {
67
- if (baseUser .isCoordinator ()){
68
- return customerAttachmentsDao .searchAll (template );
69
- }else {
70
- throw new AqualityPermissionsException ("Account is not allowed to view Customer Comments" , baseUser );
71
- }
72
- }
73
-
74
37
@ Override
75
38
public CustomerDto create (CustomerDto template ) throws AqualityException {
76
39
if (baseUser .isCoordinator ()){
77
- if (template .getId () != null && template .getAccounting () == 0 ){
78
- template .setAccount_manager (new UserDto ());
79
- template .getAccount_manager ().setId (0 );
80
- // TODO: Remove Customer Members
81
- }
82
-
83
- CustomerDto customer = fillCustomer (customerDao .create (template ));
84
-
85
- if (template .getAccounting () != null && template .getAccounting () == 1 && template .getAccount_manager () != null ){
86
- addPermissions (template .getAccount_manager ().getId (), customer );
87
- }
88
- return customer ;
40
+ return fillCustomer (customerDao .create (template ));
89
41
}else {
90
42
throw new AqualityPermissionsException ("Account is not allowed to create Customers" , baseUser );
91
43
}
92
44
}
93
- public CustomerAttachmentDto create (CustomerAttachmentDto template ) throws AqualityException {
94
- if (baseUser .isCoordinator ()){
95
- return customerAttachmentsDao .create (template );
96
- }else {
97
- throw new AqualityPermissionsException ("Account is not allowed to create Customer Attachment" , baseUser );
98
- }
99
- }
100
- public CustomerCommentDto create (CustomerCommentDto template ) throws AqualityException {
101
- if (baseUser .isCoordinator ()){
102
- return customerCommentsDao .create (template );
103
- }else {
104
- throw new AqualityPermissionsException ("Account is not allowed to create Customer Comment" , baseUser );
105
- }
106
- }
107
-
108
- // TODO: update members
109
- public void updateCustomerMember (List <CustomerMemberDto > members ){
110
- throw new UnsupportedOperationException ("Not implemented Update Members function" );
111
- }
112
45
113
46
@ Override
114
47
public boolean delete (CustomerDto template ) throws AqualityException {
@@ -118,36 +51,13 @@ public boolean delete(CustomerDto template) throws AqualityException {
118
51
throw new AqualityPermissionsException ("Account is not allowed to delete Customers" , baseUser );
119
52
}
120
53
}
121
- public boolean delete (CustomerAttachmentDto template ) throws AqualityException {
122
- if (baseUser .isCoordinator ()){
123
- FileUtils fileUtils = new FileUtils ();
124
- template = get (template ).get (0 );
125
- List <String > pathes = new ArrayList <>();
126
- pathes .add (template .getPath ());
127
- fileUtils .removeFiles (pathes );
128
- return customerAttachmentsDao .delete (template );
129
- }else {
130
- throw new AqualityPermissionsException ("Account is not allowed to view Customers" , baseUser );
131
- }
132
- }
133
54
134
55
private List <CustomerDto > fillCustomers (List <CustomerDto > customers ) throws AqualityException {
135
56
List <UserDto > users = userDao .getAll ();
136
-
137
57
for (CustomerDto customer : customers ){
138
58
customer .setCoordinator (users .stream ().filter (x -> x .getId ().equals (customer .getCoordinator_id ())).findFirst ().orElse (null ));
139
59
customer .getCoordinator ().toPublic ();
140
-
141
- if (customer .getAccounting () == 1 ){
142
- Integer accountManagerId = customer .getAccount_manager_id ();
143
- customer .setAccount_manager (users .stream ().filter (x ->x .getId ().equals (accountManagerId )).findFirst ().orElse (null ));
144
- customer .getAccount_manager ().toPublic ();
145
- }
146
-
147
60
if (baseUser .isCoordinator ()){
148
- customer .setAccount_team (getMembers (customer ));
149
- customer .setComments (getComments (customer ));
150
- customer .setAttachments (getAttachments (customer ));
151
61
customer .setProjects (getProjects (customer ));
152
62
}
153
63
}
@@ -162,60 +72,9 @@ private List<ProjectDto> getProjects(CustomerDto customer) throws AqualityExcept
162
72
return projectDao .searchAll (projectTemplate );
163
73
}
164
74
165
- private List <CustomerAttachmentDto > getAttachments (CustomerDto customer ) throws AqualityException {
166
- CustomerAttachmentDto customerAttachmentDtoTemplate = new CustomerAttachmentDto ();
167
- customerAttachmentDtoTemplate .setCustomer_id (customer .getId ());
168
- return get (customerAttachmentDtoTemplate );
169
- }
170
-
171
- private List <CustomerCommentDto > getComments (CustomerDto customer ) throws AqualityException {
172
- CustomerCommentDto customerCommentDtoTemplate = new CustomerCommentDto ();
173
- customerCommentDtoTemplate .setCustomer_id (customer .getId ());
174
- return get (customerCommentDtoTemplate );
175
- }
176
-
177
- private List <CustomerMemberDto > getMembers (CustomerDto customer ) throws AqualityException {
178
- CustomerMemberDto customerMemberDto = new CustomerMemberDto ();
179
- customerMemberDto .setCustomer_id (customer .getId ());
180
- return get (customerMemberDto );
181
- }
182
-
183
75
private CustomerDto fillCustomer (CustomerDto customer ) throws AqualityException {
184
76
List <CustomerDto > customers = new ArrayList <>();
185
77
customers .add (customer );
186
78
return fillCustomers (customers ).get (0 );
187
79
}
188
-
189
- private void addPermissions (Integer user_id , CustomerDto customerDto ) throws AqualityException {
190
- List <ProjectDto > projects = customerDto .getProjects ();
191
-
192
- for (ProjectDto project : projects ) {
193
- ProjectUserDto projectUserDto = new ProjectUserDto ();
194
- UserDto userDto = new UserDto ();
195
- userDto .setId (user_id );
196
- projectUserDto .setProject_id (project .getId ());
197
- projectUserDto .setUser (userDto );
198
- projectUserDto .setViewer (1 );
199
- projectUserDao .create (projectUserDto );
200
- }
201
- }
202
-
203
- private List <CustomerCommentDto > completeComments (List <CustomerCommentDto > comments ) throws AqualityException {
204
- UserDto userTemplate = new UserDto ();
205
-
206
- for (CustomerCommentDto comment : comments ){
207
-
208
- if (comment .getUser_id () != null ){
209
- userTemplate .setId (comment .getUser_id ());
210
- }else {
211
- userTemplate .setId (1 );
212
- }
213
- try {
214
- comment .setAuthor (userDao .getEntityById (userTemplate ));
215
- } catch (Exception e ){
216
- throw new AqualityException ("Cannot find author for Customer Comment" );
217
- }
218
- }
219
- return comments ;
220
- }
221
80
}
0 commit comments