/** * This will update the asset's content (checking it out if it is not * already). This will not save the session or create a new version of the * node (this has to be done seperately, as several properties may change as * part of one edit). This is only used if the asset is a textual asset. For * binary, use the updateBinaryContent method instead. */ public AssetItem updateContent(String newRuleContent) throws RulesRepositoryException { checkout(); try { if ( this.isBinary() ) { this.updateBinaryContentAttachment( new ByteArrayInputStream( newRuleContent.getBytes() ) ); } this.node.setProperty( CONTENT_PROPERTY_NAME, newRuleContent ); return this; } catch ( RepositoryException e ) { log.error( "Unable to update the asset content", e ); throw new RulesRepositoryException( e ); } }
@Test public void testBinaryAsset() throws Exception { AssetItem item = getRepo().loadDefaultModule().addAsset( "testBinaryAsset", "yeah" ); String data = "abc 123"; ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); item.updateBinaryContentAttachment( in ); item.updateBinaryContentAttachmentFileName( "x.x" ); in.close(); assertEquals(data, item.getContent()); assertFalse(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_NAME )); assertTrue(item.getNode().hasProperty( AssetItem.CONTENT_PROPERTY_BINARY_NAME )); item.checkin( "lalalala" ); assertTrue(item.isBinary()); item = getRepo().loadDefaultModule().loadAsset( "testBinaryAsset" ); InputStream in2 = item.getBinaryContentAttachment(); assertNotNull(in2); byte[] data2 = item.getBinaryContentAsBytes(); assertEquals(data, new String(data2)); assertEquals("x.x", item.getBinaryContentAttachmentFileName()); assertTrue(item.isBinary()); item.updateContent("qed"); item.checkin(""); item = getRepo().loadAssetByUUID(item.getUUID()); assertEquals("qed", item.getContent()); }
/** * This will update the asset's content (checking it out if it is not * already). This will not save the session or create a new version of the * node (this has to be done seperately, as several properties may change as * part of one edit). This is only used if the asset is a textual asset. For * binary, use the updateBinaryContent method instead. */ public AssetItem updateContent(String newRuleContent) throws RulesRepositoryException { checkout(); try { if ( this.isBinary() ) { this.updateBinaryContentAttachment( new ByteArrayInputStream( newRuleContent.getBytes() ) ); } this.node.setProperty( CONTENT_PROPERTY_NAME, newRuleContent ); return this; } catch ( RepositoryException e ) { log.error( "Unable to update the asset content", e ); throw new RulesRepositoryException( e ); } }
@Test public void testBinaryAssetForShareableAsset() throws Exception { AssetItem asset = getRepo().loadGlobalArea().addAsset("testBinaryAssetForShareableAsset", "desc"); asset.checkin("version0"); AssetItem linkedAsset = getDefaultPackage().addAssetImportedFromGlobalArea(asset.getName()); String data = "abc 123"; ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); linkedAsset.updateBinaryContentAttachment(in); linkedAsset.updateBinaryContentAttachmentFileName("x.x"); in.close(); assertEquals(data, linkedAsset.getContent()); assertFalse(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_NAME)); assertTrue(linkedAsset.getNode().hasProperty(AssetItem.CONTENT_PROPERTY_BINARY_NAME)); linkedAsset.checkin("lalalala"); assertTrue(linkedAsset.isBinary()); asset = getRepo().loadGlobalArea().loadAsset("testBinaryAssetForShareableAsset"); InputStream in2 = asset.getBinaryContentAttachment(); assertNotNull(in2); byte[] data2 = asset.getBinaryContentAsBytes(); assertEquals(data, new String(data2)); assertEquals("x.x", asset.getBinaryContentAttachmentFileName()); assertTrue(asset.isBinary()); linkedAsset.updateContent("qed"); linkedAsset.checkin(""); linkedAsset = getRepo().loadAssetByUUID(linkedAsset.getUUID()); assertEquals("qed", linkedAsset.getContent()); }
/** * This will restore the historical version, save, and check it in as a new * version with the given comment. * * @param versionToRestore * @param headVersion * @param comment */ public void restoreHistoricalAsset(AssetItem versionToRestore, AssetItem headVersion, String comment) { headVersion.checkout(); if (versionToRestore.isBinary()) { headVersion.updateBinaryContentAttachment(versionToRestore.getBinaryContentAttachment()); } else { headVersion.updateContent(versionToRestore.getContent()); } headVersion.checkin(comment); }
/** * This will restore the historical version, save, and check it in as a new * version with the given comment. * * @param versionToRestore * @param headVersion * @param comment */ public void restoreHistoricalAsset(AssetItem versionToRestore, AssetItem headVersion, String comment) { headVersion.checkout(); if (versionToRestore.isBinary()) { headVersion.updateBinaryContentAttachment(versionToRestore.getBinaryContentAttachment()); } else { headVersion.updateContent(versionToRestore.getContent()); } headVersion.checkin(comment); }
@Test public void testGetContentLength() throws Exception { RulesRepository repo = getRepo(); ModuleItem pkg = repo.loadDefaultModule(); AssetItem asset = pkg.addAsset("testGetContentLength", ""); assertEquals(0, asset.getContentLength()); asset.updateContent("boo"); asset.checkin(""); assertEquals("boo".getBytes().length, asset.getContentLength() ); asset = pkg.addAsset("testGetContentLength2", ""); assertEquals(0, asset.getContentLength()); asset.updateBinaryContentAttachment(new ByteArrayInputStream("foobar".getBytes())); asset.checkin(""); assertEquals("foobar".getBytes().length, asset.getContentLength()); }
asset2.updateBinaryContentAttachment(in); asset2.updateFormat("xls"); asset2.checkin("");
asset3.updateBinaryContentAttachment(in); asset3.updateFormat("xls"); asset3.checkin("");
@Test public void testGetContentLengthForShareableAsset() throws Exception { AssetItem ruleItem = loadGlobalArea().addAsset("testGetContentLengthForShareableAsset", "desc"); ruleItem.checkin("initial"); AssetItem linkedRuleItem = getDefaultPackage().addAssetImportedFromGlobalArea(ruleItem.getName()); assertEquals(0, ruleItem.getContentLength()); assertEquals(0, linkedRuleItem.getContentLength()); ruleItem.updateContent("boo"); ruleItem.checkin(""); assertEquals("boo".getBytes().length, ruleItem.getContentLength()); assertEquals("boo".getBytes().length, linkedRuleItem.getContentLength()); linkedRuleItem.updateContent("booo"); linkedRuleItem.checkin(""); assertEquals("booo".getBytes().length, ruleItem.getContentLength()); assertEquals("booo".getBytes().length, linkedRuleItem.getContentLength()); ruleItem = loadGlobalArea().addAsset("testGetContentLengthForShareableAsset2", ""); ruleItem.checkin("initial"); linkedRuleItem = getDefaultPackage().addAssetImportedFromGlobalArea(ruleItem.getName()); assertEquals(0, ruleItem.getContentLength()); assertEquals(0, linkedRuleItem.getContentLength()); linkedRuleItem.updateBinaryContentAttachment(new ByteArrayInputStream("foobar".getBytes())); linkedRuleItem.checkin(""); assertEquals("foobar".getBytes().length, ruleItem.getContentLength()); assertEquals("foobar".getBytes().length, linkedRuleItem.getContentLength()); ruleItem.updateBinaryContentAttachment(new ByteArrayInputStream("foobarr".getBytes())); ruleItem.checkin(""); assertEquals("foobarr".getBytes().length, ruleItem.getContentLength()); assertEquals("foobarr".getBytes().length, linkedRuleItem.getContentLength()); }