Skip to content
This repository was archived by the owner on Jun 7, 2024. It is now read-only.

Commit 50a5f58

Browse files
committed
file attachment plugin ajax rework finished, bugs fixed
1 parent a6c5761 commit 50a5f58

File tree

19 files changed

+276
-111
lines changed

19 files changed

+276
-111
lines changed

GeneTracker/application.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Grails Metadata file
2-
#Sat Feb 16 07:33:56 CET 2013
3-
app.grails.version=2.2.0
2+
#Fri Mar 22 11:05:51 CET 2013
3+
app.grails.version=2.2.1
44
app.name=GeneTracker
5-
plugins.hibernate=2.2.0
6-
plugins.tomcat=2.2.0
5+
plugins.hibernate=2.2.1
6+
plugins.tomcat=2.2.1

GeneTracker/grails-app/controllers/org/openlab/genetracker/ViabilityController.groovy

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package org.openlab.genetracker
22

3-
import java.util.Date;
4-
import org.openlab.security.User;
5-
import grails.converters.*;
6-
import org.openlab.data.*;
7-
import org.openlab.genetracker.*
8-
import org.springframework.dao.DataIntegrityViolationException;
3+
import org.springframework.dao.DataIntegrityViolationException
94

105
/**
116
* DataTableController for _viabilityTab.gsp
@@ -31,7 +26,6 @@ class ViabilityController{
3126
}
3227
else{
3328
def model = new org.openlab.module.tab.ViabilityModule().getModelForDomainClass("gene", params["gene.id"])
34-
println model
3529
render(template: "/tabs/viabilityTab", model: model)
3630
}
3731
}

OpenLabAttachments/OpenLabAttachments.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<sourceFolder url="file://$MODULE_DIR$/grails-app/domain" isTestSource="false" />
3535
<sourceFolder url="file://$MODULE_DIR$/grails-app/taglib" isTestSource="false" />
3636
<sourceFolder url="file://$MODULE_DIR$/test/unit" isTestSource="true" />
37+
<sourceFolder url="file://$MODULE_DIR$/grails-app/services" isTestSource="false" />
3738
<excludeFolder url="file://$MODULE_DIR$/target/classes" />
3839
</content>
3940
<orderEntry type="inheritedJdk" />
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Grails Metadata file
2-
#Sat Feb 16 07:34:51 CET 2013
3-
app.grails.version=2.2.0
2+
#Fri Mar 22 11:06:50 CET 2013
3+
app.grails.version=2.2.1
44
app.name=OpenLabAttachments
5-
plugins.hibernate=2.2.0
6-
plugins.tomcat=2.2.0
5+
plugins.hibernate=2.2.1
6+
plugins.tomcat=2.2.1

OpenLabAttachments/grails-app/conf/BuildConfig.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@ grails.project.dependency.resolution = {
3030

3131
// runtime 'mysql:mysql-connector-java:5.1.13'
3232
}
33+
34+
plugins{
35+
compile ":ajax-uploader:1.1"
36+
}
3337
}
3438
grails.plugin.location.OpenLabBackend = "../OpenLabBackend"

OpenLabAttachments/grails-app/conf/Config.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ log4j = {
2222

2323
warn 'org.mortbay.log'
2424
}
25-
grails.views.default.codec="none" // none, html, base64
26-
grails.views.gsp.encoding="UTF-8"
25+
grails.views.default.codec="none" // none, html, base64
26+
grails.views.gsp.encoding="UTF-8"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
modules = {
2+
overrides{
3+
fileuploader{
4+
resource id: '/css/uploader.css', url: '/css/custom_uploader.css'
5+
}
6+
}
7+
}

OpenLabAttachments/grails-app/controllers/openlab/attachments/DataObjectAttachmentController.groovy

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import org.springframework.web.multipart.MultipartFile
1212
class DataObjectAttachmentController {
1313

1414
def searchableService
15-
def attachmentableService
15+
def fileUploadHelperService
1616
def ajaxUploaderService
1717
def grailsApplication
1818

@@ -78,21 +78,17 @@ class DataObjectAttachmentController {
7878
*/
7979

8080
def uploadFile = {
81+
//create temporary file
8182
def timestamp = new java.util.Date().time
82-
83-
def uploadedTempFile = new File(grailsApplication.config.openlab?.upload?.dir?:"" + "upload" + timestamp + ".tmp")
84-
85-
println uploadedTempFile
86-
if(!uploadedTempFile.canWrite()){
87-
return render(text: [success: false] as JSON, contentType: 'text/json')
88-
}
83+
def uploadedTempFile = new File(((grailsApplication.config.openlab?.upload?.dir?:"") + "upload" + timestamp + ".tmp"))
8984

9085
try{
91-
if (request instanceof MultipartHttpServletRequest) {
92-
MultipartFile uploadedFile = ((MultipartHttpServletRequest) request).getFile('qqfile')
93-
ajaxUploaderService.upload(uploadedFile.inputStream, uploadedTempFile)
94-
}
95-
} catch(Exception e){
86+
if(!new File(grailsApplication.config.openlab?.upload?.dir?:"").canWrite()){
87+
return render(text: [success: false] as JSON, contentType: 'text/json')
88+
}
89+
90+
ajaxUploaderService.upload((InputStream) fileUploadHelperService.selectInputStream(request), uploadedTempFile)
91+
} catch(FileUploadException e){
9692
log.error("Failed to upload file.", e)
9793
return render(text: [success:false] as JSON, contentType:'text/json')
9894
}
@@ -103,61 +99,31 @@ class DataObjectAttachmentController {
10399

104100
def createWithAddin = {
105101

106-
def doaInstance = new DataObjectAttachment();
107-
108-
//extract attachmentLink_* from params. it contains necessary information about which dataObject to attach to.
109-
for (String key in params.keySet()) {
110-
if (key.startsWith("attachmentLink_")) {
102+
//create one dataobjectattachment for each file that has been uploaded
103+
for(int currentFile = 0; currentFile <= params.int("filesUploaded"); currentFile++){
104+
println params."fileName_${currentFile}"
111105

112-
def keySplitArray = key.split("_")
113-
def geneAndId = keySplitArray[1]
106+
//fill a dataobject attachment instance with linked data object
107+
DataObjectAttachment doaInstance = fileUploadHelperService.createDataObjectAttachmentInstance(params)
114108

115-
def geneAndIdSplitArray = geneAndId.split(":")
116-
def id = Long.valueOf(geneAndIdSplitArray[1])
117-
def domainName = geneAndIdSplitArray[0]
118-
119-
def splitArray = params[key].split(":")
120-
121-
//def domainName = splitArray[0]
122-
def name = splitArray[1]
123-
124-
def domainInstance = grailsApplication.getArtefactByLogicalPropertyName("Domain", domainName.toString())?.getClazz()?.get(id)
125-
126-
if (domainInstance)
127-
doaInstance.addToDataObjects(domainInstance)
109+
//rename the temporary uploaded file to a permanent file and link it to the doaInstance
110+
doaInstance = fileUploadHelperService.processTempFile(params."filePath_${currentFile}", params."fileName_${currentFile}", doaInstance)
111+
if(!doaInstance){
112+
flash.message = "Failed to process uploaded file"
113+
render template: "../addins/attachmentsAddin", layout: "body"
114+
return
128115
}
129-
}
130-
131-
132-
//create attachments for these files using the dataObjects extracted above.
133-
def f = new File(params.tempFile)
134116

135-
if (f.canRead()) {
136-
def timestamp = new java.util.Date()
137-
138-
doaInstance.setFileUploadDate(timestamp)
139-
140-
String folder = grailsApplication.config.openlab?.upload?.dir?: ""
141-
String newPath = folder + timestamp.getTime().toString() + "_" + params.fileName
142-
doaInstance.setFileName(params.fileName)
143-
def fileArray = params.fileName.toString().split("\\.")
144-
doaInstance.setFileType(fileArray[fileArray.length - 1].toUpperCase())
145-
doaInstance.setPathToFile(newPath)
146-
doaInstance.setDescription("")
147-
148-
f.renameTo(new File(newPath))
149-
}
150-
else{
151-
render "<div class='message'>Failed to read file</div>"
152-
return
153-
}
154-
155-
if (!doaInstance.save(flush: true)) {
156-
render "<div class='message'>Save failed</div>"
157-
return
117+
//save it
118+
else if (!doaInstance.save(flush: true)) {
119+
flash.message = "Save failed"
120+
render template: "../addins/attachmentsAddin", layout: "body"
121+
return
122+
}
158123
}
159124

160-
render "<div class='message'>Save successful!</div>"
125+
flash.message = "Save successful"
126+
render template: "../addins/attachmentsAddin", layout: "body"
161127
}
162128

163129
/**
@@ -297,4 +263,49 @@ class DataObjectAttachmentController {
297263
redirect(action: "list")
298264
}
299265
}
266+
267+
def deleteWithinTab = {
268+
println params
269+
def dataObjectAttachmentInstance = DataObjectAttachment.get(params.id)
270+
if (dataObjectAttachmentInstance) {
271+
try {
272+
File file = new File(dataObjectAttachmentInstance.getPathToFile());
273+
274+
if (file.exists()) {
275+
if (file.canWrite()) {
276+
file.delete();
277+
}
278+
else {
279+
render (text: [success: false, message: "Could not write on file!"] as JSON, contentType:'text/json')
280+
}
281+
282+
dataObjectAttachmentInstance.delete(flush: true)
283+
flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'dataObjectAttachment.label', default: 'DataObjectAttachment'), params.id])}"
284+
}
285+
else {
286+
dataObjectAttachmentInstance.delete(flush: true)
287+
}
288+
289+
render (text: [success: false, message: "File does not exist, deleted only the database entry."] as JSON, contentType:'text/json')
290+
}
291+
catch (org.springframework.dao.DataIntegrityViolationException e) {
292+
def message = "${message(code: 'default.not.deleted.message', args: [message(code: 'dataObjectAttachment.label', default: 'DataObjectAttachment'), params.id])}"
293+
render (text: [success: false, message: message] as JSON, contentType:'text/json')
294+
}
295+
catch (IOException ioe) {
296+
def message = "${message(code: 'default.not.deleted.message', args: [message(code: 'dataObjectAttachment.label', default: 'DataObjectAttachment'), params.id])}"
297+
render (text: [success: false, message: message] as JSON , contentType:'text/json')
298+
}
299+
}
300+
else {
301+
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'dataObjectAttachment.label', default: 'DataObjectAttachment'), params.id])}"
302+
render (text: [success: true] as JSON, contentType:'text/json')
303+
}
304+
}
305+
306+
def renderAttachmentsTab = {
307+
def module = new org.openlab.module.tab.AttachmentsTabModule()
308+
def model = module.getModelForDomainClass("", params.id)
309+
render template: "../tabs/attachmentsTab", model: model
310+
}
300311
}

OpenLabAttachments/grails-app/modules/org/openlab/module/tab/AttachmentsTabModule.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class AttachmentsTabModule implements Module{
3636
createAlias("dataObjects", "dos")
3737
eq("dos.id", Long.valueOf(id))
3838
}
39-
return [dataObjectAttachmentInstanceList: attachments, dataObjectAttachmentInstanceTotal: attachments?.size()?:0]
39+
return [dataObjectAttachmentInstanceList: attachments, dataObject: dataObject, dataObjectAttachmentInstanceTotal: attachments?.size()?:0]
4040
}
4141

4242
def isMobile()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package openlabattachments
2+
3+
import javax.servlet.http.HttpServletRequest
4+
import org.springframework.web.multipart.MultipartHttpServletRequest
5+
import org.springframework.web.multipart.MultipartFile
6+
import openlab.attachments.DataObjectAttachment
7+
8+
class FileUploadHelperService {
9+
10+
def grailsApplication
11+
12+
InputStream selectInputStream(HttpServletRequest request) {
13+
if (request instanceof MultipartHttpServletRequest) {
14+
MultipartFile uploadedFile = ((MultipartHttpServletRequest) request).getFile('qqfile')
15+
return uploadedFile.inputStream
16+
}
17+
return request.inputStream
18+
}
19+
20+
DataObjectAttachment createDataObjectAttachmentInstance(def params) {
21+
def doaInstance = new DataObjectAttachment();
22+
23+
//extract attachmentLink_* from params. it contains necessary information about which dataObject to attach to.
24+
for (String key in params.keySet()) {
25+
if (key.startsWith("attachmentLink_")) {
26+
27+
def keySplitArray = key.split("_")
28+
def geneAndId = keySplitArray[1]
29+
30+
def geneAndIdSplitArray = geneAndId.split(":")
31+
def id = Long.valueOf(geneAndIdSplitArray[1])
32+
def domainName = geneAndIdSplitArray[0]
33+
34+
def splitArray = params[key].split(":")
35+
36+
//def domainName = splitArray[0]
37+
def name = splitArray[1]
38+
39+
def domainInstance = grailsApplication.getArtefactByLogicalPropertyName("Domain", domainName.toString())?.getClazz()?.get(id)
40+
41+
if (domainInstance)
42+
doaInstance.addToDataObjects(domainInstance)
43+
}
44+
}
45+
doaInstance
46+
}
47+
48+
DataObjectAttachment processTempFile(tempFile, fileName, doaInstance)
49+
{
50+
def f = new File(tempFile)
51+
52+
if (f.canRead()) {
53+
def timestamp = new java.util.Date()
54+
55+
doaInstance.setFileUploadDate(timestamp)
56+
57+
String folder = grailsApplication.config.openlab?.upload?.dir?: ""
58+
String newPath = folder + timestamp.getTime().toString() + "_" + fileName
59+
doaInstance.setFileName(fileName)
60+
def fileArray = fileName.toString().split("\\.")
61+
doaInstance.setFileType(fileArray[fileArray.length - 1].toUpperCase())
62+
doaInstance.setPathToFile(newPath)
63+
doaInstance.setDescription("")
64+
65+
f.renameTo(new File(newPath))
66+
return doaInstance
67+
}
68+
else return null
69+
}
70+
}

0 commit comments

Comments
 (0)