Skip to content

Instantly share code, notes, and snippets.

@dulichan
Last active December 20, 2015 17:49
Show Gist options
  • Save dulichan/6171023 to your computer and use it in GitHub Desktop.
Save dulichan/6171023 to your computer and use it in GitHub Desktop.
Check the createArtifact method
(function (server, registry) {
var log = new Log();
var GenericArtifactManager = Packages.org.wso2.carbon.governance.api.generic.GenericArtifactManager;
var GenericArtifactFilter = Packages.org.wso2.carbon.governance.api.generic.GenericArtifactFilter;
var ByteArrayInputStream = Packages.java.io.ByteArrayInputStream;
var QName = Packages.javax.xml.namespace.QName;
var IOUtils = Packages.org.apache.commons.io.IOUtils;
var buildArtifact = function (manager, artifact) {
var data = {
id: String(artifact.id),
type: String(manager.type),
name: String(artifact.getAttribute('overview_name')),
path: "/_system/governance" + String(artifact.getPath()),
lifecycle: artifact.getLifecycleName(),
lifecycleState: artifact.getLifecycleState(),
mediaType: String(artifact.getMediaType()),
attributes: (function () {
var i, name,
names = artifact.getAttributeKeys(),
length = names.length,
attributes = {};
for (i = 0; i < length; i++) {
name = names[i];
attributes[name] = String(artifact.getAttribute(name));
}
return attributes;
}())};
if(artifact.getContent()!=null){
data.content = function () {
return new Stream(new ByteArrayInputStream(artifact.getContent()));
};
}
return data;
};
var createArtifact = function (manager, options) {
var name, attribute, i, length, lc,
artifact = manager.newGovernanceArtifact(new QName(options.name)),
attributes = options.attributes;
for (name in attributes) {
if (attributes.hasOwnProperty(name)) {
attribute = attributes[name];
if (attribute instanceof Array) {
/*length = attribute.length;
for (i = 0; i < length; i++) {
artifact.addAttribute(name, attribute[i]);
}*/
artifact.setAttributes(name, attribute);
} else {
artifact.setAttribute(name, attribute);
}
}
}
if (options.id) {
artifact.id = options.id;
}
if (options.content) {
if (options.content instanceof Stream) {
artifact.setContent(IOUtils.toByteArray(options.content.getStream()));
} else {
artifact.setContent(new java.lang.String(options.content).getBytes());
}
}
lc = options.lifecycles;
if (lc) {
length = lc.length;
for (i = 0; i < length; i++) {
artifact.attachLifecycle(lc[i]);
}
}
return artifact;
};
var ArtifactManager = function (registry, type) {
this.registry = registry;
var governanceRegistry = Packages.org.wso2.carbon.governance.api.util.GovernanceUtils.getGovernanceUserRegistry(registry.registry, user);
this.manager = new GenericArtifactManager(governanceRegistry, type);
this.type = type;
};
registry.ArtifactManager = ArtifactManager;
ArtifactManager.prototype.find = function (fn, paging) {
var i, length, artifacts,
artifactz = [];
artifacts = this.manager.findGenericArtifacts(new GenericArtifactFilter({
matches: function (artifact) {
return fn(buildArtifact(this, artifact));
}
}));
length = artifacts.length;
for (i = 0; i < length; i++) {
artifactz.push(buildArtifact(this, artifacts[i]));
}
return artifactz;
};
ArtifactManager.prototype.get = function (id) {
return buildArtifact(this, this.manager.getGenericArtifact(id));
};
ArtifactManager.prototype.count = function () {
return this.manager.getAllGenericArtifactIds().length;
};
ArtifactManager.prototype.list = function (paging) {
var i,
artifactz = [],
artifacts = this.manager.getAllGenericArtifacts(),
length = artifacts.length;
for (i = 0; i < length; i++) {
artifactz.push(buildArtifact(this, artifacts[i]));
}
return artifactz;
};
/*
{
name: 'AndroidApp1',
attributes: {
overview_status: "CREATED",
overview_name: 'AndroidApp1',
overview_version: '1.0.0',
overview_url: 'http://overview.com',
overview_provider: 'admin',
images_thumbnail: 'http://localhost:9763/portal/gadgets/co2-emission/thumbnail.jpg',
images_banner: 'http://localhost:9763/portal/gadgets/electric-power/banner.jpg'
},
lifecycles : ['lc1', 'lc2'],
content : '<?xml ....>'
}
*/
ArtifactManager.prototype.add = function (options) {
this.manager.addGenericArtifact(createArtifact(this.manager, options));
};
ArtifactManager.prototype.update = function (options) {
var obj = createArtifact(this.manager, options);
this.manager.updateGenericArtifact(obj);
};
ArtifactManager.prototype.remove = function (id) {
this.manager.removeGenericArtifact(id);
};
}(server, registry));
var screenshot1 = files['screenshot1File'];
screenshot1 = saveFile(screenshot1);
var screenshot2 = files['screenshot2File'];
screenshot2 = saveFile(screenshot2);
var screenshot3 = files['screenshot3File'];
screenshot3 = saveFile(screenshot3);
var screenshots = [screenshot1, screenshot2, screenshot3];
var path = rxtPath + ctx.provider + '/' +ctx.name+ '/' +ctx.version;
if(ctx.packagename == undefined ||ctx.packagename == ""){
ctx.packagename = "com.wso2mobile.wso2phonedirectory";
}
// Packages.org.wso2.carbon.governance.api.util.GovernanceUtils.loadGovernanceArtifacts(registry.registry);
am = new carbon.registry.ArtifactManager(registry, "mobileapp");
var attributes ={
overview_status: "PENDING-REVIEW",
overview_name: ctx.name,
overview_platform:t,
overview_version: ctx.version,
overview_url: ctx.url,
overview_provider: ctx.provider,
overview_description: ctx.description,
overview_market:ctx.markettype,
// overview_recentChanges:ctx.recentChanges,
overview_packageName:ctx.packagename,
overview_category:ctx.category,
images_icon:icon,
images_banner:banner,
images_screenshots:screenshots //Culprit
};
attributes[attributes.overview_status+"_date"]=printDate();
am.add({
name: ctx.name,
lifecycle:["applifecycle"],
path:path,
attributes:attributes
});
<screenshots>one</screenshots>
<screenshots>two</screenshots>
<screenshots>three</screenshots>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment