/
AnnexableService.groovy
264 lines (235 loc) · 8.13 KB
/
AnnexableService.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package blocks
import grails.transaction.Transactional
import org.apache.commons.logging.LogFactory
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.StandardMultipartFile
import blocks.exceptions.EmptyDomainObjectException
import blocks.exceptions.InsufficientParamsException
@Transactional
class AnnexableService {
private static final log = LogFactory.getLog(AnnexableService.class)
/**
* Method for get attached annexes for domain object
* @param domainObject Object to check
* @param params Additional params like order, max or offset
* @return Collection of attached annexes
*/
def getAnnexesForDomain(def domainObject, def params = [:]) {
if (!domainObject) {
throw new EmptyDomainObjectException()
}
if (!domainObject.ident()) {
throw new EmptyDomainObjectException("No identity for domain object")
}
params.order = params.order ?: 'desc'
params.cache = true
Object result = AnnexableDomain.createCriteria().list(params) {
eq 'domainName', domainObject.class.name
eq 'domainId', domainObject.ident()
}
result?.getAt('annex')
}
def getAnnex(AnnexableDomain annexableDomain, def params = [:]) {
annexableDomain.annex
}
def getAnnexInfo(Long annexId, String fileName, String bucket) {
Annex annex = Annex.get(annexId)
def result = [:]
result.id = annex.id
result.fileName = annex.fileName
result.bucket = annex.bucket
result.isDeleted = annex.isDeleted
result.createdAt = annex.createdAt
result.createdBy = annex.createdBy
result.editedAt = annex.editedAt
result.editedBy = annex.editedBy
result.extension = annex.extension
result.size = annex.length ?: 0
result.contentType = annex.contentType ?: 'unknown'
result.versions = (annex.fileVersion + 1)
return result
}
def getAnnexesGroupByBucket() {
def result = Annex.listOrderByBucket().groupBy { annex ->
annex.bucket
}
log.debug(result)
result
}
def getAllBuckets() {
def result = Annex.createCriteria().list() {
projections {
distinct('bucket')
}
}
log.info(resultMap)
result
}
int getAnnexesCount(def domainObject) {
if (!domainObject) {
throw new EmptyDomainObjectException()
}
if (!domainObject.ident()) {
throw new EmptyDomainObjectException("No identity for domain object")
}
int result = AnnexableDomain.createCriteria().get() {
projections {
rowCount()
}
eq 'domainName', domainObject.class.name
eq 'domainId', domainObject.ident()
}
result
}
int getAnnexesCount() {
int result = AnnexableDomain.createCriteria().get() {
projections {
rowCount()
}
}
result
}
def find(String namePart, String bucket, def params= [:]) {
params.order = params.order ?: 'desc'
params.cache = true
def results = Annex.createCriteria().list(params) {
ilike('fileName', "%${namePart}%")
}
results
}
def downloadAnnexFile(Long annexId, Long versionToDownload) {
Annex annex = Annex.get(annexId)
downloadAnnexFile(annex, versionToDownload)
}
def downloadAnnexFile(Annex annex, Long versionToDownload) {
if (!annex) {
throw new EmptyDomainObjectException("Annex cannot be null")
}
if (!versionToDownload) {
versionToDownload = annex.fileVersion
}
def params = [:]
params.versionToDownload = versionToDownload
def file = FileRepo.getFile(annex, params)
file
}
def attach(String domainName, Long domainId, Long annexId) {
if (!domainName) {
throw new EmptyDomainObjectException()
}
if (!domainId) {
throw new EmptyDomainObjectException("No identity for domain object")
}
if (!annexId) {
throw new InsufficientParamsException()
}
Annex annex = Annex.get(annexId)
AnnexableDomain annexableDomain = new AnnexableDomain()
annexableDomain.annex = annex
annexableDomain.domainName = domainName
annexableDomain.domainId = domainId
annex.addToAnnexableDomains(annexableDomain)
annex.save()
return annex
}
def attach(def domainObject, Long annexId) {
Annex annex = Annex.get(annexId)
attach(domainObject, annex)
}
def attach(def domainObject, Annex annex) {
if (!domainObject) {
throw new EmptyDomainObjectException()
}
if (!domainObject.ident()) {
throw new EmptyDomainObjectException("No identity for domain object")
}
AnnexableDomain annexableDomain = new AnnexableDomain()
annexableDomain.annex = annex
annexableDomain.domainName = domainObject.class.name
annexableDomain.domainId = domainObject.ident()
annex.addToAnnexableDomains(annexableDomain)
annex.save()
return annex
}
def addAnnex(def domainObject, StandardMultipartFile file) {
if (!domainObject) {
throw new EmptyDomainObjectException()
}
if (!domainObject.ident()) {
throw new EmptyDomainObjectException("No identity for domain object")
}
add(file, domainObject)
}
def add(Annex annex) {
if (annex.file) {
annex = FileRepo.uploadFile(annex)
}
annex
}
def add(Annex annex, boolean copyLocalFile) {
if (annex.file) {
annex = FileRepo.uploadFile(annex, [:], copyLocalFile)
}
return annex
}
def add(def file, def domainObject) {
if (!domainObject) {
throw new EmptyDomainObjectException()
}
if (!domainObject.ident()) {
throw new EmptyDomainObjectException("No identity for domain object")
}
add(file, domainObject.class.name, domainObject.ident())
}
def add(def file, String domainName, Long domainId) {
Annex annex = new Annex()
annex.fileName = file.filename
AnnexableDomain annexableDomain = new AnnexableDomain()
annexableDomain.annex = annex
annexableDomain.domainName = domainName
annexableDomain.domainId = domainId
annex.addToAnnexableDomains(annexableDomain)
annex.save()
annex.file = file
add(annex)
}
def detach(def domainObject, Long annexId) {
detach(domainObject?.class?.name, domainObject?.ident(), annexId)
}
def detach(String domainName, Long domainId, Long annexId) {
Annex annex = Annex.get(annexId)
boolean isDeleted = false
if (annex) {
def matchingAnnexableDomains = annex.annexableDomains.findAll {
it.domainName == domainName && it.domainId == domainId
}
for (AnnexableDomain annexableDomain : matchingAnnexableDomains) {
annex.removeFromAnnexableDomains(annexableDomain)
annexableDomain.delete(flush: true)
}
/*for (int i = 0; i < matchingAnnexableDomains.size(); i++) {
matchingAnnexableDomains.get(i).delete(flush: true)
}*/
//isDeleted = annex.annexableDomains.removeAll { it.domainName == domainName && it.domainId == domainId}
}
annex.save flush:true
true
}
def moveToTrash(Map params=[:]) {
if (!params.annexId) {
return
}
Annex annex = Annex.get(params.annexId)
if (!annex) {
return
}
params.bucket = params.bucket ?: annex.bucket
params.version = params.version ?: annex.fileVersion
boolean ret = FileRepo.moveToTrash(params)
annex.isDeleted = true
annex.save flush:true
ret
}
def emptyTrash() {
FileRepo.emptyTrash()
}
}