Native query for GET /document/id

This commit is contained in:
jendib 2015-05-10 13:45:39 +02:00
parent 1e3282eff3
commit b7f920f864
5 changed files with 67 additions and 21 deletions

View File

@ -68,11 +68,29 @@ public class DocumentDao {
* @param id Document ID * @param id Document ID
* @return Document * @return Document
*/ */
public Document getDocument(String id) { public DocumentDto getDocument(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select d from Document d where d.id = :id and d.deleteDate is null"); StringBuilder sb = new StringBuilder("select d.DOC_ID_C, d.DOC_TITLE_C, d.DOC_DESCRIPTION_C, d.DOC_CREATEDATE_D, d.DOC_LANGUAGE_C, ");
sb.append(" (select count(s.SHA_ID_C) from T_SHARE s, T_ACL ac where ac.ACL_SOURCEID_C = d.DOC_ID_C and ac.ACL_TARGETID_C = s.SHA_ID_C and ac.ACL_DELETEDATE_D is null and s.SHA_DELETEDATE_D is null), ");
sb.append(" (select count(f.FIL_ID_C) from T_FILE f where f.FIL_DELETEDATE_D is null and f.FIL_IDDOC_C = d.DOC_ID_C), ");
sb.append(" u.USE_USERNAME_C ");
sb.append(" from T_DOCUMENT d, T_USER u ");
sb.append(" where d.DOC_IDUSER_C = u.USE_ID_C and d.DOC_ID_C = :id and d.DOC_DELETEDATE_D is null ");
Query q = em.createNativeQuery(sb.toString());
q.setParameter("id", id); q.setParameter("id", id);
return (Document) q.getSingleResult(); Object[] o = (Object[]) q.getSingleResult();
DocumentDto documentDto = new DocumentDto();
int i = 0;
documentDto.setId((String) o[i++]);
documentDto.setTitle((String) o[i++]);
documentDto.setDescription((String) o[i++]);
documentDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
documentDto.setLanguage((String) o[i++]);
documentDto.setShared(((Number) o[i++]).intValue() > 0);
documentDto.setFileCount(((Number) o[i++]).intValue());
documentDto.setCreator((String) o[i++]);
return documentDto;
} }
/** /**

View File

@ -44,6 +44,11 @@ public class DocumentDto {
*/ */
private Integer fileCount; private Integer fileCount;
/**
* Document creator.
*/
private String creator;
/** /**
* Getter de id. * Getter de id.
* *
@ -167,4 +172,20 @@ public class DocumentDto {
public void setFileCount(Integer fileCount) { public void setFileCount(Integer fileCount) {
this.fileCount = fileCount; this.fileCount = fileCount;
} }
/**
* Getter of creator.
* @return creator
*/
public String getCreator() {
return creator;
}
/**
* Setter of creator.
* @param creator creator
*/
public void setCreator(String creator) {
this.creator = creator;
}
} }

View File

@ -38,7 +38,6 @@ import com.sismics.docs.core.dao.jpa.AclDao;
import com.sismics.docs.core.dao.jpa.DocumentDao; import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.FileDao; import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.TagDao; import com.sismics.docs.core.dao.jpa.TagDao;
import com.sismics.docs.core.dao.jpa.UserDao;
import com.sismics.docs.core.dao.jpa.criteria.DocumentCriteria; import com.sismics.docs.core.dao.jpa.criteria.DocumentCriteria;
import com.sismics.docs.core.dao.jpa.dto.AclDto; import com.sismics.docs.core.dao.jpa.dto.AclDto;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto; import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
@ -84,10 +83,9 @@ public class DocumentResource extends BaseResource {
DocumentDao documentDao = new DocumentDao(); DocumentDao documentDao = new DocumentDao();
AclDao aclDao = new AclDao(); AclDao aclDao = new AclDao();
UserDao userDao = new UserDao(); DocumentDto documentDto;
Document documentDb;
try { try {
documentDb = documentDao.getDocument(documentId); documentDto = documentDao.getDocument(documentId);
// Check document visibility // Check document visibility
if (!aclDao.checkPermission(documentId, PermType.READ, shareId == null ? principal.getId() : shareId)) { if (!aclDao.checkPermission(documentId, PermType.READ, shareId == null ? principal.getId() : shareId)) {
@ -98,12 +96,13 @@ public class DocumentResource extends BaseResource {
} }
JSONObject document = new JSONObject(); JSONObject document = new JSONObject();
document.put("id", documentDb.getId()); document.put("id", documentDto.getId());
document.put("title", documentDb.getTitle()); document.put("title", documentDto.getTitle());
document.put("description", documentDb.getDescription()); document.put("description", documentDto.getDescription());
document.put("create_date", documentDb.getCreateDate().getTime()); document.put("create_date", documentDto.getCreateTimestamp());
document.put("language", documentDb.getLanguage()); document.put("language", documentDto.getLanguage());
// TODO Add "shared" and "file_count" -> rewrite the query in SQL document.put("shared", documentDto.getShared());
document.put("file_count", documentDto.getFileCount());
if (principal.isAnonymous()) { if (principal.isAnonymous()) {
// No tags in anonymous mode (sharing) // No tags in anonymous mode (sharing)
@ -125,7 +124,7 @@ public class DocumentResource extends BaseResource {
// Below is specific to GET /document/id // Below is specific to GET /document/id
document.put("creator", userDao.getById(documentDb.getUserId()).getUsername()); document.put("creator", documentDto.getCreator());
// Add ACL // Add ACL
List<AclDto> aclDtoList = aclDao.getBySourceId(documentId); List<AclDto> aclDtoList = aclDao.getBySourceId(documentId);

View File

@ -41,6 +41,7 @@ import com.sismics.docs.core.dao.jpa.AclDao;
import com.sismics.docs.core.dao.jpa.DocumentDao; import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.FileDao; import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.UserDao; import com.sismics.docs.core.dao.jpa.UserDao;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
import com.sismics.docs.core.event.FileCreatedAsyncEvent; import com.sismics.docs.core.event.FileCreatedAsyncEvent;
import com.sismics.docs.core.event.FileDeletedAsyncEvent; import com.sismics.docs.core.event.FileDeletedAsyncEvent;
import com.sismics.docs.core.model.context.AppContext; import com.sismics.docs.core.model.context.AppContext;
@ -486,9 +487,9 @@ public class FileResource extends BaseResource {
// Get the document // Get the document
DocumentDao documentDao = new DocumentDao(); DocumentDao documentDao = new DocumentDao();
Document document; DocumentDto documentDto;
try { try {
document = documentDao.getDocument(documentId); documentDto = documentDao.getDocument(documentId);
// Check document visibility // Check document visibility
AclDao aclDao = new AclDao(); AclDao aclDao = new AclDao();
@ -537,7 +538,7 @@ public class FileResource extends BaseResource {
// Write to the output // Write to the output
return Response.ok(stream) return Response.ok(stream)
.header("Content-Type", "application/zip") .header("Content-Type", "application/zip")
.header("Content-Disposition", "attachment; filename=\"" + document.getTitle().replaceAll("\\W+", "_") + ".zip\"") .header("Content-Disposition", "attachment; filename=\"" + documentDto.getTitle().replaceAll("\\W+", "_") + ".zip\"")
.build(); .build();
} }
} }

View File

@ -138,8 +138,8 @@ public class TestDocumentResource extends BaseJerseyTest {
postParams.add("title", "My super title document 1"); postParams.add("title", "My super title document 1");
postParams.add("description", "My super description for document 1"); postParams.add("description", "My super description for document 1");
postParams.add("language", "eng"); postParams.add("language", "eng");
create1Date = new Date().getTime(); long create3Date = new Date().getTime();
postParams.add("create_date", create1Date); postParams.add("create_date", create3Date);
response = documentResource.put(ClientResponse.class, postParams); response = documentResource.put(ClientResponse.class, postParams);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class); json = response.getEntity(JSONObject.class);
@ -148,14 +148,14 @@ public class TestDocumentResource extends BaseJerseyTest {
// Add a file // Add a file
fileResource = resource().path("/file"); fileResource = resource().path("/file");
fileResource.addFilter(new CookieAuthenticationFilter(document1Token)); fileResource.addFilter(new CookieAuthenticationFilter(document3Token));
form = new FormDataMultiPart(); form = new FormDataMultiPart();
file = this.getClass().getResourceAsStream("/file/Einstein-Roosevelt-letter.png"); file = this.getClass().getResourceAsStream("/file/Einstein-Roosevelt-letter.png");
fdp = new FormDataBodyPart("file", fdp = new FormDataBodyPart("file",
new BufferedInputStream(file), new BufferedInputStream(file),
MediaType.APPLICATION_OCTET_STREAM_TYPE); MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(fdp); form.bodyPart(fdp);
form.field("id", document1Id); form.field("id", document3Id);
response = fileResource.type(MediaType.MULTIPART_FORM_DATA).put(ClientResponse.class, form); response = fileResource.type(MediaType.MULTIPART_FORM_DATA).put(ClientResponse.class, form);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class); json = response.getEntity(JSONObject.class);
@ -206,6 +206,13 @@ public class TestDocumentResource extends BaseJerseyTest {
json = response.getEntity(JSONObject.class); json = response.getEntity(JSONObject.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
Assert.assertEquals(document1Id, json.getString("id")); Assert.assertEquals(document1Id, json.getString("id"));
Assert.assertEquals("document1", json.getString("creator"));
Assert.assertEquals(1, json.getInt("file_count"));
Assert.assertEquals(true, json.getBoolean("shared"));
Assert.assertEquals("My super title document 1", json.getString("title"));
Assert.assertEquals("My super description for document 1", json.getString("description"));
Assert.assertEquals("eng", json.getString("language"));
Assert.assertEquals(create1Date, json.getLong("create_date"));
tags = json.getJSONArray("tags"); tags = json.getJSONArray("tags");
Assert.assertEquals(1, tags.length()); Assert.assertEquals(1, tags.length());
Assert.assertEquals(tag1Id, tags.getJSONObject(0).getString("id")); Assert.assertEquals(tag1Id, tags.getJSONObject(0).getString("id"));