-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementação do upload de arquivos no cadastro de componentes digitais
- Loading branch information
Guilherme Andrade Del Cantoni
committed
Apr 3, 2017
1 parent
cdf8042
commit 2814e1b
Showing
12 changed files
with
587 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
grails-app/controllers/working/docweb/ComponenteDigitalController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package working.docweb | ||
|
||
import java.security.MessageDigest | ||
|
||
import static org.springframework.http.HttpStatus.* | ||
import grails.transaction.Transactional | ||
|
||
@Transactional(readOnly = true) | ||
class ComponenteDigitalController { | ||
|
||
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"] | ||
|
||
def index(Integer max) { | ||
params.max = Math.min(max ?: 10, 100) | ||
respond ComponenteDigital.list(params), model:[componenteDigitalCount: ComponenteDigital.count()] | ||
} | ||
|
||
def show(ComponenteDigital componenteDigital) { | ||
respond componenteDigital | ||
} | ||
|
||
def create() { | ||
respond new ComponenteDigital(params) | ||
} | ||
|
||
@Transactional | ||
def save(ComponenteDigital componenteDigital) { | ||
if (componenteDigital == null) { | ||
transactionStatus.setRollbackOnly() | ||
notFound() | ||
return | ||
} | ||
|
||
def file = request.getFile('myFile') | ||
if (file != null && !file.empty) { | ||
componenteDigital.nomeOriginal = file.originalFilename | ||
componenteDigital.formato = file.contentType | ||
componenteDigital.tamanho = file.size | ||
componenteDigital.fixidade = MessageDigest.getInstance("SHA-256").digest(file.bytes).encodeHex().toString() | ||
componenteDigital.armazenamento = "/tmp/${UUID.randomUUID() as String}" | ||
file.transferTo(new File(componenteDigital.armazenamento)) | ||
} | ||
|
||
if (!componenteDigital.validate()) { | ||
transactionStatus.setRollbackOnly() | ||
respond componenteDigital.errors, view:'create' | ||
return | ||
} | ||
|
||
componenteDigital.save flush:true | ||
|
||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.created.message', args: [message(code: 'componenteDigital.label', default: 'ComponenteDigital'), componenteDigital.id]) | ||
redirect componenteDigital | ||
} | ||
'*' { respond componenteDigital, [status: CREATED] } | ||
} | ||
} | ||
|
||
def edit(ComponenteDigital componenteDigital) { | ||
respond componenteDigital | ||
} | ||
|
||
@Transactional | ||
def update(ComponenteDigital componenteDigital) { | ||
if (componenteDigital == null) { | ||
transactionStatus.setRollbackOnly() | ||
notFound() | ||
return | ||
} | ||
|
||
//throw new Exception(request.class.toString()) | ||
def file = request.getFile('myFile') | ||
if (file != null && !file.empty) { | ||
componenteDigital.nomeOriginal = file.originalFilename | ||
componenteDigital.formato = file.contentType | ||
componenteDigital.tamanho = file.size | ||
componenteDigital.fixidade = MessageDigest.getInstance("SHA-256").digest(file.bytes).encodeHex().toString() | ||
componenteDigital.armazenamento = "/tmp/${UUID.randomUUID() as String}" | ||
file.transferTo(new File(componenteDigital.armazenamento)) | ||
} | ||
|
||
if (componenteDigital.validate()) { | ||
transactionStatus.setRollbackOnly() | ||
respond componenteDigital.errors, view:'edit' | ||
return | ||
} | ||
|
||
componenteDigital.save flush:true | ||
|
||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.updated.message', args: [message(code: 'componenteDigital.label', default: 'ComponenteDigital'), componenteDigital.id]) | ||
redirect componenteDigital | ||
} | ||
'*'{ respond componenteDigital, [status: OK] } | ||
} | ||
} | ||
|
||
@Transactional | ||
def delete(ComponenteDigital componenteDigital) { | ||
|
||
if (componenteDigital == null) { | ||
transactionStatus.setRollbackOnly() | ||
notFound() | ||
return | ||
} | ||
|
||
componenteDigital.delete flush:true | ||
|
||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.deleted.message', args: [message(code: 'componenteDigital.label', default: 'ComponenteDigital'), componenteDigital.id]) | ||
redirect action:"index", method:"GET" | ||
} | ||
'*'{ render status: NO_CONTENT } | ||
} | ||
} | ||
|
||
protected void notFound() { | ||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.not.found.message', args: [message(code: 'componenteDigital.label', default: 'ComponenteDigital'), params.id]) | ||
redirect action: "index", method: "GET" | ||
} | ||
'*'{ render status: NOT_FOUND } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package working.docweb | ||
|
||
class ComponenteDigital { | ||
|
||
ComponenteDigital(Documento documento){ | ||
this.documento = documento | ||
} | ||
|
||
/*** | ||
* Referência para o documento ao qual o componente digital pertence | ||
*/ | ||
Documento documento | ||
|
||
/*** | ||
* Nome original do componente digital no momento em que foi inserido no repositório, | ||
* antes de ser renomeado com o identificador do repositório. | ||
*/ | ||
String nomeOriginal | ||
|
||
/*** | ||
* Propriedades técnicas de um componente digital, aplicáveis à maioria dos formatos, | ||
* tais como: nível de composição, tamanho, software de criação e inibidores. | ||
*/ | ||
String caracteristicasTecnicas | ||
|
||
/*** | ||
* Identificação do formato de arquivo do componente digital | ||
* | ||
* | ||
*/ | ||
String formato | ||
|
||
/*** | ||
* Informações sobre a localização e o suporte do componente digital, bem como os recursos necessários para | ||
* armazenamento permanente. | ||
*/ | ||
String armazenamento | ||
|
||
/*** | ||
* Informações sobre o ambiente de software necessário para apresentar e/ou usar os componentes digitais, | ||
* incluindo a aplicação e o sistema operacional. | ||
*/ | ||
String ambienteSofware | ||
|
||
/*** | ||
* Informações sobre os componentes de hardware necessários para operar o software referenciado em 5.6, | ||
* incluindo periféricos. | ||
*/ | ||
String ambienteHardware | ||
|
||
/*** | ||
* Informações utilizadas para verificar se o componente digital sofreu mudanças não documentadas | ||
*/ | ||
String fixidade | ||
|
||
/*** | ||
* Tamanho, em bytes, do espaço ocupado pela componente digital no sistema de arquivo | ||
*/ | ||
Long tamanho = 0 | ||
|
||
static belongsTo = [documento: Documento] | ||
|
||
static constraints = { | ||
nomeOriginal blank: false, size: 3..100 | ||
formato blank: false | ||
armazenamento blank: false | ||
fixidade blank: false, size: 64..64 | ||
caracteristicasTecnicas nullable: true | ||
ambienteSofware nullable: true | ||
ambienteHardware nullable: true | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="layout" content="main" /> | ||
<g:set var="entityName" value="${message(code: 'componenteDigital.label', default: 'ComponenteDigital')}" /> | ||
<title><g:message code="default.create.label" args="[entityName]" /></title> | ||
</head> | ||
<body> | ||
<a href="#create-componenteDigital" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content…"/></a> | ||
<div class="nav" role="navigation"> | ||
<ul> | ||
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li> | ||
<li><g:link class="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li> | ||
</ul> | ||
</div> | ||
<div id="create-componenteDigital" class="content scaffold-create" role="main"> | ||
<h1><g:message code="default.create.label" args="[entityName]" /></h1> | ||
<g:if test="${flash.message}"> | ||
<div class="message" role="status">${flash.message}</div> | ||
</g:if> | ||
<g:hasErrors bean="${this.componenteDigital}"> | ||
<ul class="errors" role="alert"> | ||
<g:eachError bean="${this.componenteDigital}" var="error"> | ||
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li> | ||
</g:eachError> | ||
</ul> | ||
</g:hasErrors> | ||
<g:uploadForm action="save"> | ||
<fieldset class="form"> | ||
<f:field bean="componenteDigital" property="documento"/> | ||
Upload Form: <input type="file" name="myFile" /> | ||
</fieldset> | ||
<fieldset class="buttons"> | ||
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" /> | ||
</fieldset> | ||
</g:uploadForm> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="layout" content="main" /> | ||
<g:set var="entityName" value="${message(code: 'componenteDigital.label', default: 'ComponenteDigital')}" /> | ||
<title><g:message code="default.edit.label" args="[entityName]" /></title> | ||
</head> | ||
<body> | ||
<a href="#edit-componenteDigital" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content…"/></a> | ||
<div class="nav" role="navigation"> | ||
<ul> | ||
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li> | ||
<li><g:link class="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li> | ||
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li> | ||
</ul> | ||
</div> | ||
<div id="edit-componenteDigital" class="content scaffold-edit" role="main"> | ||
<h1><g:message code="default.edit.label" args="[entityName]" /></h1> | ||
<g:if test="${flash.message}"> | ||
<div class="message" role="status">${flash.message}</div> | ||
</g:if> | ||
<g:hasErrors bean="${this.componenteDigital}"> | ||
<ul class="errors" role="alert"> | ||
<g:eachError bean="${this.componenteDigital}" var="error"> | ||
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li> | ||
</g:eachError> | ||
</ul> | ||
</g:hasErrors> | ||
<g:uploadForm resource="${this.componenteDigital}" method="POST"> | ||
<g:hiddenField name="version" value="${this.componenteDigital?.version}" /> | ||
<fieldset class="form"> | ||
<f:field bean="componenteDigital" property="documento"/> | ||
Upload Form: <input type="file" name="myFile" /> | ||
</fieldset> | ||
<fieldset class="buttons"> | ||
<input class="save" type="submit" value="${message(code: 'default.button.update.label', default: 'Update')}" /> | ||
</fieldset> | ||
</g:uploadForm> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="layout" content="main" /> | ||
<g:set var="entityName" value="${message(code: 'componenteDigital.label', default: 'ComponenteDigital')}" /> | ||
<title><g:message code="default.list.label" args="[entityName]" /></title> | ||
</head> | ||
<body> | ||
<a href="#list-componenteDigital" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content…"/></a> | ||
<div class="nav" role="navigation"> | ||
<ul> | ||
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li> | ||
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li> | ||
</ul> | ||
</div> | ||
<div id="list-componenteDigital" class="content scaffold-list" role="main"> | ||
<h1><g:message code="default.list.label" args="[entityName]" /></h1> | ||
<g:if test="${flash.message}"> | ||
<div class="message" role="status">${flash.message}</div> | ||
</g:if> | ||
<f:table collection="${componenteDigitalList}" /> | ||
|
||
<div class="pagination"> | ||
<g:paginate total="${componenteDigitalCount ?: 0}" /> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
Oops, something went wrong.