1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 17:56:20 +00:00

Sequential files upload, basic search system

This commit is contained in:
jendib
2013-07-29 00:04:34 +02:00
parent 51434752f5
commit cd97382f60
12 changed files with 153 additions and 40 deletions

View File

@@ -20,6 +20,7 @@ import javax.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.google.common.base.Strings;
import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.criteria.DocumentCriteria;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
@@ -86,7 +87,8 @@ public class DocumentResource extends BaseResource {
@QueryParam("limit") Integer limit,
@QueryParam("offset") Integer offset,
@QueryParam("sort_column") Integer sortColumn,
@QueryParam("asc") Boolean asc) throws JSONException {
@QueryParam("asc") Boolean asc,
@QueryParam("search") String search) throws JSONException {
if (!authenticate()) {
throw new ForbiddenClientException();
}
@@ -99,6 +101,9 @@ public class DocumentResource extends BaseResource {
SortCriteria sortCriteria = new SortCriteria(sortColumn, asc);
DocumentCriteria documentCriteria = new DocumentCriteria();
documentCriteria.setUserId(principal.getId());
if (!Strings.isNullOrEmpty(search)) {
documentCriteria.setSearch(search);
}
documentDao.findByCriteria(paginatedList, documentCriteria, sortCriteria);
for (DocumentDto documentDto : paginatedList.getResultList()) {

View File

@@ -22,7 +22,8 @@ App.controller('Document', function($scope, $state, Restangular) {
offset: $scope.offset,
limit: $scope.limit,
sort_column: $scope.sortColumn,
asc: $scope.asc
asc: $scope.asc,
search: $scope.search
})
.then(function(data) {
$scope.documents = data;
@@ -47,6 +48,12 @@ App.controller('Document', function($scope, $state, Restangular) {
$scope.pageDocuments();
});
$scope.$watch('search', function(prev, next) {
if (next) {
$scope.loadDocuments();
}
})
/**
* Sort documents.
*/

View File

@@ -41,41 +41,58 @@ App.controller('DocumentEdit', function($scope, $q, $http, $state, $stateParams,
var promises = [];
$scope.fileProgress = 0;
_.each($scope.newFiles, function(file) {
// Build the payload
var formData = new FormData();
formData.append('id', data.id);
formData.append('file', file);
// Send the file
var promiseFile = $http.put('api/file',
formData, {
headers: { 'Content-Type': false },
transformRequest: function(data) { return data; }
});
// TODO Handle progression when $q.notify will be released
promiseFile.then(function() {
$scope.fileProgress += 100 / _.size($scope.newFiles);
});
// Store the promise for later
promises.push(promiseFile);
});
// When all files upload are over, move on
var promiseAll = $q.all(promises);
if ($scope.isEdit()) {
promiseAll.then(function(data) {
var navigateNext = function() {
if ($scope.isEdit()) {
$scope.loadDocuments();
$state.transitionTo('document.view', { id: $stateParams.id });
});
} else {
promiseAll.then(function(data) {
} else {
$scope.document = {};
$scope.loadDocuments();
});
}
}
if (_.size($scope.newFiles) == 0) {
navigateNext();
} else {
$scope.fileIsUploading = true;
// Send a file from the input file array and return a promise
var sendFile = function(key) {
// Build the payload
var file = $scope.newFiles[key];
var formData = new FormData();
formData.append('id', data.id);
formData.append('file', file);
// Send the file
var promiseFile = $http.put('api/file',
formData, {
headers: { 'Content-Type': false },
transformRequest: function(data) { return data; }
});
// TODO Handle progression when $q.notify will be released
promiseFile.then(function() {
$scope.fileProgress += 100 / _.size($scope.newFiles);
});
return promiseFile;
};
// Upload files sequentially
var key = 0;
var then = function() {
key++;
if ($scope.newFiles[key]) {
sendFile(key).then(then);
} else {
$scope.fileIsUploading = false;
navigateNext();
}
};
sendFile(key).then(then);
}
});
};

View File

@@ -3,17 +3,43 @@
/**
* Document view controller.
*/
App.controller('DocumentView', function($rootScope, $scope, $state, $stateParams, Restangular) {
App.controller('DocumentView', function($rootScope, $scope, $state, $stateParams, $dialog, Restangular) {
// Load data from server
$scope.document = Restangular.one('document', $stateParams.id).get();
Restangular.one('file').getList('list', { id: $stateParams.id }).then(function(data) {
$rootScope.files = data.files;
});
/**
* Load files from server.
*/
$scope.loadFiles = function() {
Restangular.one('file').getList('list', { id: $stateParams.id }).then(function(data) {
$rootScope.files = data.files;
});
};
$scope.loadFiles();
/**
* Navigate to the selected file.
*/
$scope.openFile = function(file) {
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: file.id })
};
/**
* Delete a file.
*/
$scope.deleteFile = function(file) {
var title = 'Delete file';
var msg = 'Do you really want to delete this file?';
var btns = [{result:'cancel', label: 'Cancel'}, {result:'ok', label: 'OK', cssClass: 'btn-primary'}];
$dialog.messageBox(title, msg, btns)
.open()
.then(function(result) {
if (result == 'ok') {
Restangular.one('file', file.id).remove().then(function() {
$scope.loadFiles();
});
}
});
}
});

View File

@@ -23,6 +23,7 @@
</div>
</form>
<div class="row-fluid">
<div class="span12"><progress percent="fileProgress" class="progress-info active"></progress></div>
<div class="row-fluid" ng-show="fileIsUploading">
<h4>Uploading files...</h4>
<div class="span6"><progress percent="fileProgress" class="progress-info active"></progress></div>
</div>

View File

@@ -4,6 +4,10 @@
<p class="text-center">
<button class="btn btn-primary" type="button" ng-click="addDocument()">Add a document</button>
</p>
<p class="input-prepend text-center input-block-level">
<span class="add-on"><span class="icon-search"></span></span>
<input class="span10" type="text" placeholder="Search" ng-model="search" >
</p>
<table class="table table-striped table-hover table-documents">
<thead>
<tr>

View File

@@ -18,7 +18,7 @@
</a>
<div class="caption">
<p class="text-right">
<button class="btn btn-danger" ng-click="deleteFile(file.id)"><span class="icon-trash icon-white"></span></button>
<button class="btn btn-danger" ng-click="deleteFile(file)"><span class="icon-trash icon-white"></span></button>
</p>
</div>
</div>

View File

@@ -55,6 +55,29 @@ public class TestDocumentResource extends BaseJerseyTest {
Assert.assertTrue(documents.length() == 1);
Assert.assertEquals(document1Id, documents.getJSONObject(0).getString("id"));
// Search documents
documentResource = resource().path("/document/list");
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
getParams = new MultivaluedMapImpl();
getParams.putSingle("search", "Sup");
response = documentResource.queryParams(getParams).get(ClientResponse.class);
json = response.getEntity(JSONObject.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
documents = json.getJSONArray("documents");
Assert.assertTrue(documents.length() == 1);
Assert.assertEquals(document1Id, documents.getJSONObject(0).getString("id"));
// Search documents (nothing)
documentResource = resource().path("/document/list");
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
getParams = new MultivaluedMapImpl();
getParams.putSingle("search", "random");
response = documentResource.queryParams(getParams).get(ClientResponse.class);
json = response.getEntity(JSONObject.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
documents = json.getJSONArray("documents");
Assert.assertTrue(documents.length() == 0);
// Get a document
documentResource = resource().path("/document/" + document1Id);
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));