/** @return */ private NodeRef checkout() { // Check out the node NodeRef workingCopy = cociService.checkout( this.nodeRef, this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("workingCopy")); assertNotNull(workingCopy); // System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef)); // Ensure that the working copy and copy aspect has been applied assertTrue(nodeService.hasAspect(workingCopy, ContentModel.ASPECT_WORKING_COPY)); assertTrue(nodeService.hasAspect(workingCopy, ContentModel.ASPECT_COPIEDFROM)); // Check that the working copy owner has been set correctly assertEquals( this.userNodeRef, nodeService.getProperty(workingCopy, ContentModel.PROP_WORKING_COPY_OWNER)); // Check that the working copy name has been set correctly String name = (String) this.nodeService.getProperty(this.nodeRef, PROP_NAME_QNAME); String expectedWorkingCopyLabel = I18NUtil.getMessage("coci_service.working_copy_label"); String expectedWorkingCopyName = ((CheckOutCheckInServiceImpl) this.cociService) .createWorkingCopyName(name, expectedWorkingCopyLabel); String workingCopyName = (String) this.nodeService.getProperty(workingCopy, PROP_NAME_QNAME); assertEquals(expectedWorkingCopyName, workingCopyName); // Check a record has been kept of the working copy label used to create the working copy name assertEquals( "No record of working copy label kept", expectedWorkingCopyLabel, nodeService.getProperty(workingCopy, ContentModel.PROP_WORKING_COPY_LABEL)); // Ensure that the content has been copied correctly ContentReader contentReader = this.contentService.getReader(this.nodeRef, ContentModel.PROP_CONTENT); assertNotNull(contentReader); ContentReader contentReader2 = this.contentService.getReader(workingCopy, ContentModel.PROP_CONTENT); assertNotNull(contentReader2); assertEquals( "The content string of the working copy should match the original immediatly after checkout.", contentReader.getContentString(), contentReader2.getContentString()); return workingCopy; }
public String getPersonDescription() { ContentService cs = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getContentService(); ContentReader reader = cs.getReader(this.person.getNodeRef(), ContentModel.PROP_PERSONDESC); if (reader != null && reader.exists()) { return Utils.stripUnsafeHTMLTags(reader.getContentString()).replace("\r\n", "<p>"); } else { return null; } }
private JSONObject getPreferencesObject(String userName) throws JSONException { JSONObject jsonPrefs = null; // Get the user node reference NodeRef personNodeRef = this.personService.getPerson(userName); if (personNodeRef == null) { throw new AlfrescoRuntimeException( "Cannot get preferences for " + userName + " because he/she does not exist."); } String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser(); boolean isSystem = AuthenticationUtil.isRunAsUserTheSystemUser() || authenticationContext.isSystemUserName(currentUserName); if (isSystem || userName.equals(currentUserName) || personService .getUserIdentifier(userName) .equals(personService.getUserIdentifier(currentUserName)) || authorityService.isAdminAuthority(currentUserName)) { // Check for preferences aspect if (this.nodeService.hasAspect(personNodeRef, ContentModel.ASPECT_PREFERENCES) == true) { // Get the preferences for this user ContentReader reader = this.contentService.getReader(personNodeRef, ContentModel.PROP_PREFERENCE_VALUES); if (reader != null) { jsonPrefs = new JSONObject(reader.getContentString()); } } } else { // The current user does not have sufficient permissions to get // the preferences for this user throw new AccessDeniedException( "The current user " + currentUserName + " does not have sufficient permissions to get the preferences of the user " + userName); } return jsonPrefs; }
/** Upload an encoded Base64 file to the repository and decode it back once it's uploaded. */ @Override protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { HashMap<String, Object> model = new HashMap<String, Object>(); UserTransaction trx = serviceRegistry.getTransactionService().getUserTransaction(); try { trx.begin(); System.out.println(trx.hashCode()); Element args = Arguments.getArguments(req); String username = args.getElementsByTagName(FORM_PARAM_USERNAME).item(0).getFirstChild().getNodeValue(); model.put(FTL_USERNAME, username); Impersonate.impersonate(username); String ref = DocumentUtils.pujarDocumentBase64(req, args, username).trim(); NodeRef nodeRef = new NodeRef(ref); Map<QName, Serializable> props = serviceRegistry.getNodeService().getProperties(nodeRef); ContentReader reader = serviceRegistry.getContentService().getReader(nodeRef, ContentModel.PROP_CONTENT); byte[] contentDecoded = es.mityc.firmaJava.libreria.utilidades.Base64.decode(reader.getContentString()); ContentWriter writer = serviceRegistry.getContentService().getWriter(nodeRef, ContentModel.PROP_CONTENT, true); writer.putContent(new ByteArrayInputStream(contentDecoded)); serviceRegistry.getOwnableService().setOwner(nodeRef, username); Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); ScriptNode document = new ScriptNode(nodeRef, serviceRegistry, scope); model.put(FTL_DOCUMENT, document); model.put(FTL_SUCCESS, String.valueOf(true)); // Auditar creación de documento String type = document.getTypeShort(); String site = document.getSiteShortName(); if (type.equals("udl:documentSimple")) { AuditUdl.auditRecord( auditComponent, username, document.getNodeRef().toString(), AUDIT_ACTION_CREATE_DOCUMENT_SIMPLE, type, site); QName qNameIdDocSimple = QName.createQName( "http://www.smile.com/model/udl/1.0", "secuencial_identificador_documentSimple"); String idDocSimple = (String) serviceRegistry.getNodeService().getProperty(nodeRef, qNameIdDocSimple); if ("".equals(idDocSimple) || idDocSimple == null) { // serviceRegistry.getNodeService().deleteNode(nodeRef); throw new Exception("Error obtenint identificador via WebService."); } } trx.commit(); } catch (Exception e) { e.printStackTrace(); model.put(FTL_ERROR, e.getMessage()); model.put(FTL_SUCCESS, String.valueOf(false)); try { if (trx.getStatus() == javax.transaction.Status.STATUS_ACTIVE) { System.out.println(trx.hashCode()); trx.rollback(); } } catch (SystemException ex) { e.printStackTrace(); } } return model; }
@Override protected Map<String, Object> executeImpl( SiteInfo site, String pageTitle, WebScriptRequest req, JSONObject json, Status status, Cache cache) { Map<String, Object> model = new HashMap<>(); // Grab the version string Map<String, String> templateVars = req.getServiceMatch().getTemplateVars(); String versionId = templateVars.get("versionId"); if (versionId == null) { String error = "No versionId supplied"; throw new WebScriptException(Status.STATUS_BAD_REQUEST, error); } // Try to find the page WikiPageInfo page = wikiService.getWikiPage(site.getShortName(), pageTitle); if (page == null) { String message = "The Wiki Page could not be found"; status.setCode(Status.STATUS_NOT_FOUND); status.setMessage(message); // Return an empty string though model.put(PARAM_CONTENT, ""); return model; } // Fetch the version history for the node VersionHistory versionHistory = null; Version version = null; try { versionHistory = versionService.getVersionHistory(page.getNodeRef()); } catch (AspectMissingException e) { } if (versionHistory == null) { // Not been versioned, return an empty string model.put(PARAM_CONTENT, ""); return model; } // Fetch the version by either ID or Label Matcher m = LABEL_PATTERN.matcher(versionId); if (m.matches()) { // It's a version label like 2.3 try { version = versionHistory.getVersion(versionId); } catch (VersionDoesNotExistException e) { } } else { // It's a version ID like ed00bac1-f0da-4042-8598-45a0d39cb74d // (The ID is usually part of the NodeRef of the frozen node, but we // don't assume to be able to just generate the full NodeRef) for (Version v : versionHistory.getAllVersions()) { if (v.getFrozenStateNodeRef().getId().equals(versionId)) { version = v; } } } // Did we find the right version in the end? String contents; if (version != null) { ContentReader reader = contentService.getReader(version.getFrozenStateNodeRef(), ContentModel.PROP_CONTENT); if (reader != null) { contents = reader.getContentString(); } else { // No content was stored in the version history contents = ""; } } else { // No warning of the missing version, just return an empty string contents = ""; } // All done model.put(PARAM_CONTENT, contents); model.put("page", page); model.put("site", site); model.put("siteId", site.getShortName()); return model; }
/** Test checkIn */ public void testCheckIn() { NodeRef workingCopy = checkout(); // Test standard check-in Map<String, Serializable> versionProperties = new HashMap<String, Serializable>(); versionProperties.put(Version.PROP_DESCRIPTION, "This is a test version"); cociService.checkin(workingCopy, versionProperties); // Test check-in with content NodeRef workingCopy3 = checkout(); nodeService.setProperty(workingCopy3, PROP_NAME_QNAME, TEST_VALUE_2); nodeService.setProperty(workingCopy3, PROP2_QNAME, TEST_VALUE_3); ContentWriter tempWriter = this.contentService.getWriter(workingCopy3, ContentModel.PROP_CONTENT, false); assertNotNull(tempWriter); tempWriter.putContent(CONTENT_2); String contentUrl = tempWriter.getContentUrl(); Map<String, Serializable> versionProperties3 = new HashMap<String, Serializable>(); versionProperties3.put(Version.PROP_DESCRIPTION, "description"); versionProperties3.put(VersionModel.PROP_VERSION_TYPE, VersionType.MAJOR); NodeRef origNodeRef = cociService.checkin(workingCopy3, versionProperties3, contentUrl, true); assertNotNull(origNodeRef); // Check the checked in content ContentReader contentReader = this.contentService.getReader(origNodeRef, ContentModel.PROP_CONTENT); assertNotNull(contentReader); assertEquals(CONTENT_2, contentReader.getContentString()); // Check that the version history is correct Version version = this.versionService.getCurrentVersion(origNodeRef); assertNotNull(version); assertEquals("description", version.getDescription()); assertEquals(VersionType.MAJOR, version.getVersionType()); NodeRef versionNodeRef = version.getFrozenStateNodeRef(); assertNotNull(versionNodeRef); // Check the verioned content ContentReader versionContentReader = this.contentService.getReader(versionNodeRef, ContentModel.PROP_CONTENT); assertNotNull(versionContentReader); assertEquals(CONTENT_2, versionContentReader.getContentString()); // Check that the name is not updated during the check-in assertEquals(TEST_VALUE_2, nodeService.getProperty(versionNodeRef, PROP_NAME_QNAME)); assertEquals(TEST_VALUE_2, nodeService.getProperty(origNodeRef, PROP_NAME_QNAME)); // Check that the other properties are updated during the check-in assertEquals(TEST_VALUE_3, nodeService.getProperty(versionNodeRef, PROP2_QNAME)); assertEquals(TEST_VALUE_3, nodeService.getProperty(origNodeRef, PROP2_QNAME)); // Cancel the check out after is has been left checked out cociService.cancelCheckout(workingCopy3); // Test keep checked out flag NodeRef workingCopy2 = checkout(); Map<String, Serializable> versionProperties2 = new HashMap<String, Serializable>(); versionProperties2.put(Version.PROP_DESCRIPTION, "Another version test"); this.cociService.checkin(workingCopy2, versionProperties2, null, true); this.cociService.checkin(workingCopy2, new HashMap<String, Serializable>(), null, true); }