DocumentManagerBean implements IDocumentManagerLocal { @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public BinaryResource saveFileInTemplate(DocumentMasterTemplateKey pDocMTemplateKey, String pName, long pSize) throws WorkspaceNotFoundException, NotAllowedException, DocumentMasterTemplateNotFoundException, FileAlreadyExistsException, UserNotFoundException, UserNotActiveException, CreationException, AccessRightException, WorkspaceNotEnabledException { User user = userManager.checkWorkspaceReadAccess(pDocMTemplateKey.getWorkspaceId()); checkNameFileValidity(pName); DocumentMasterTemplate template = documentMasterTemplateDAO.loadDocMTemplate(pDocMTemplateKey); checkDocumentTemplateWriteAccess(template, user); BinaryResource binaryResource = null; String fullName = template.getWorkspaceId() + "/document-templates/" + template.getId() + "/" + pName; for (BinaryResource bin : template.getAttachedFiles()) { if (bin.getFullName().equals(fullName)) { binaryResource = bin; break; } } if (binaryResource == null) { binaryResource = new BinaryResource(fullName, pSize, new Date()); binaryResourceDAO.createBinaryResource(binaryResource); template.addFile(binaryResource); } else { binaryResource.setContentLength(pSize); binaryResource.setLastModified(new Date()); } return binaryResource; }  @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override BinaryResource saveFileInTemplate(DocumentMasterTemplateKey pDocMTemplateKey, String pName, long pSize); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override BinaryResource saveFileInDocument(DocumentIterationKey pDocPK, String pName, long pSize); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void setDocumentPublicShared(DocumentRevisionKey pDocRPK, boolean isPublicShared); @LogDocument @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override BinaryResource getBinaryResource(String pFullName); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override BinaryResource getTemplateBinaryResource(String pFullName); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override String[] getFolders(String pCompletePath); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] findDocumentRevisionsByFolder(String pCompletePath); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] findDocumentRevisionsByTag(TagKey pKey); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override DocumentRevision getDocumentRevision(DocumentRevisionKey pDocRPK); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override DocumentIteration findDocumentIterationByBinaryResource(BinaryResource pBinaryResource); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void updateDocumentACL(String pWorkspaceId, DocumentRevisionKey docKey, Map<String, String> pACLUserEntries, Map<String, String> pACLUserGroupEntries); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void updateACLForDocumentMasterTemplate(String pWorkspaceId, String pDocMTemplateId, Map<String, String> userEntries, Map<String, String> userGroupEntries); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void removeACLFromDocumentRevision(DocumentRevisionKey documentRevisionKey); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void removeACLFromDocumentMasterTemplate(String pWorkspaceId, String documentTemplateId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] getAllDocumentsInWorkspace(String workspaceId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] getFilteredDocumentsInWorkspace(String workspaceId, int start, int pMaxResults); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override int getDocumentsInWorkspaceCount(String workspaceId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] getCheckedOutDocumentRevisions(String pWorkspaceId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override Task[] getTasks(String pWorkspaceId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevisionKey[] getIterationChangeEventSubscriptions(String pWorkspaceId); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override DocumentRevisionKey[] getStateChangeEventSubscriptions(String pWorkspaceId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override boolean isUserStateChangeEventSubscribedForGivenDocument(String pWorkspaceId, DocumentRevision docR); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override boolean isUserIterationChangeEventSubscribedForGivenDocument(String pWorkspaceId, DocumentRevision docR); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] getDocumentRevisionsWithReferenceOrTitle(String pWorkspaceId, String search, int maxResults); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override String generateId(String pWorkspaceId, String pDocMTemplateId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] searchDocumentRevisions(DocumentSearchQuery pQuery, int from, int size); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentMasterTemplate[] getDocumentMasterTemplates(String pWorkspaceId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentMasterTemplate getDocumentMasterTemplate(DocumentMasterTemplateKey pKey); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentMasterTemplate updateDocumentMasterTemplate(DocumentMasterTemplateKey pKey, String pDocumentType, String pWorkflowModelId, String pMask, List<InstanceAttributeTemplate> pAttributeTemplates, String[] lovNames, boolean idGenerated, boolean attributesLocked); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void deleteTag(TagKey pKey); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void createTag(String pWorkspaceId, String pLabel); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision createDocumentMaster(String pParentFolder, String pDocMId, String pTitle, String pDescription, String pDocMTemplateId, String pWorkflowModelId, Map<String, String> aclUserEntries, Map<String, String> aclGroupEntries, Map<String, Collection<String>> userRoleMapping, Map<String, Collection<String>> groupRoleMapping); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override DocumentRevision[] getAllCheckedOutDocumentRevisions(String pWorkspaceId); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentMasterTemplate createDocumentMasterTemplate(String pWorkspaceId, String pId, String pDocumentType, String pWorkflowModelId,
                                                               String pMask, List<InstanceAttributeTemplate> pAttributeTemplates, String[] lovNames, boolean idGenerated, boolean attributesLocked); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision moveDocumentRevision(String pParentFolder, DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override Folder createFolder(String pParentFolder, String pFolder); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision checkOutDocument(DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision saveTags(DocumentRevisionKey pDocRPK, String[] pTags); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision removeTag(DocumentRevisionKey pDocRPK, String pTag); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision undoCheckOutDocument(DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision checkInDocument(DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevisionKey[] deleteFolder(String pCompletePath); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevisionKey[] deleteUserFolder(User pUser); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevisionKey[] moveFolder(String pCompletePath, String pDestParentFolder, String pDestFolder); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void deleteDocumentRevision(DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void deleteDocumentMasterTemplate(DocumentMasterTemplateKey pKey); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision removeFileFromDocument(String pFullName); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override BinaryResource renameFileInDocument(String pFullName, String pNewName); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentMasterTemplate removeFileFromTemplate(String pFullName); @Override BinaryResource renameFileInTemplate(String pFullName, String pNewName); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision updateDocument(DocumentIterationKey iKey, String pRevisionNote, List<InstanceAttribute> pAttributes, DocumentRevisionKey[] pLinkKeys, String[] documentLinkComments); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] createDocumentRevision(DocumentRevisionKey pOriginalDocRPK, String pTitle, String pDescription, String pWorkflowModelId, Map<String, String> aclUserEntries, Map<String, String> aclGroupEntries, Map<String, Collection<String>> userRoleMapping, Map<String, Collection<String>> groupRoleMapping); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void subscribeToStateChangeEvent(DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void unsubscribeToStateChangeEvent(DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void subscribeToIterationChangeEvent(DocumentRevisionKey pDocRPK); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void unsubscribeToIterationChangeEvent(DocumentRevisionKey pDocRPK); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override String[] getTags(String pWorkspaceId); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override long getDiskUsageForDocumentsInWorkspace(String pWorkspaceId); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override long getDiskUsageForDocumentTemplatesInWorkspace(String pWorkspaceId); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override SharedDocument createSharedDocument(DocumentRevisionKey pDocRPK, String pPassword, Date pExpireDate); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override void deleteSharedDocument(SharedEntityKey sharedEntityKey); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override boolean canAccess(DocumentRevisionKey docRKey); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override boolean canAccess(DocumentIterationKey docIKey); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override boolean canUserAccess(User user, DocumentRevisionKey docRKey); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID, UserGroupMapping.ADMIN_ROLE_ID}) @Override boolean canUserAccess(User user, DocumentIterationKey docIKey); @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) @Override List<DocumentIteration> getInverseDocumentsLink(DocumentRevisionKey docKey); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] getDocumentRevisionsWithAssignedTasksForGivenUser(String pWorkspaceId, String assignedUserLogin); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision[] getDocumentRevisionsWithOpenedTasksForGivenUser(String pWorkspaceId, String assignedUserLogin); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override void createDocumentLog(DocumentLog log); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision releaseDocumentRevision(DocumentRevisionKey pRevisionKey); @RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override DocumentRevision markDocumentRevisionAsObsolete(DocumentRevisionKey pRevisionKey); @Override void logDocument(String fullName, String event);  }
@Test public void saveFileInTemplate() throws Exception { DocumentMasterTemplateKey pDocMTemplateKey = Mockito.spy(new DocumentMasterTemplateKey(DocumentUtil.WORKSPACE_ID, DocumentUtil.DOCUMENT_ID)); Mockito.when(userManager.checkWorkspaceWriteAccess(DocumentUtil.WORKSPACE_ID)).thenReturn(user); Mockito.when(userManager.checkWorkspaceReadAccess(DocumentUtil.WORKSPACE_ID)).thenReturn(user); Mockito.when(documentMasterTemplateDAO.loadDocMTemplate(pDocMTemplateKey)).thenReturn(documentMasterTemplate); BinaryResource binaryResource = documentManagerBean.saveFileInTemplate(pDocMTemplateKey, DocumentUtil.FILE1_NAME, DocumentUtil.DOCUMENT_SIZE); Assert.assertNotNull(binaryResource.getLastModified()); Assert.assertEquals(DocumentUtil.DOCUMENT_SIZE, binaryResource.getContentLength()); Assert.assertTrue(!binaryResource.getFullName().isEmpty()); Assert.assertEquals(binaryResource.getFullName(), DocumentUtil.WORKSPACE_ID + "/document-templates/" + DocumentUtil.DOCUMENT_TEMPLATE_ID + "/" + DocumentUtil.FILE1_NAME); }