Is there a way to add more information to a node, except the mandatory ones? - corda

I want to add more node information to a network node. Is it possible to share more data besides what's in the node configuration file? Maybe some custom fields, like an encoded logo image or stuff like that.
Thanks

Yes you can.
Inside your module under src folder add a file called config.conf.
Add your values inside of it in the following format:
key1="string_value"
key2=number_value
Inside build.gradle go to the part where you define your nodes, let's say your module name is "my_module"; do this:
cordapp (project(':my_module')) {
config project.file("src/config.conf")
}
Now when you run deployNodes, gradle will generate a file called my_module.conf under build\nodes\my_node\cordapps\config.
To access those values inside your flow:
getServiceHub().getAppContext().getConfig().getString("key1");
As for testing flows; to mimic the custom config file you need to do the following:
Map<String, String> customConfig = new HashMap<>();
customConfig.put("key1", "string_value");
customConfig.put("key2", "int_value");
// Setup network.
network = new MockNetwork(new MockNetworkParameters().withCordappsForAllNodes(ImmutableList.of(
TestCordapp.findCordapp("my_package").withConfig(customConfig))));

Related

Accessing custom project flavor property stored in .csproj file

OK, so I've managed to create a custom project flavor with a custom property page. It all works and the values are being saved to the .csproj file like such:
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{880389B4-B814-4796-844B-F0E1678C31D1}" Configuration="Debug|Any CPU">
<ServiceLibraryProjectFlavorCfg>
<BooleanProperty>True</BooleanProperty>
</ServiceLibraryProjectFlavorCfg>
</FlavorProperties>
<FlavorProperties GUID="{880389B4-B814-4796-844B-F0E1678C31D1}" Configuration="Release|Any CPU">
<ServiceLibraryProjectFlavorCfg />
</FlavorProperties>
</VisualStudio>
What I cant seem to figure out is how to access this custom property from, say, a menu item callback in my package. I can get the project that the selected item in the solution explorer which was right clicked belongs to, but I'm stuck after that...
Any help will be appreciated
Thanx
Hein
OK, I figured it out.
As part of creating a custom project flavor, you inherit from FlavoredProjectBase and implement the IVsProjectFlavorCfgProvider interface.
the IVsProjectFlavorCfgProvider has one implementable method
int CreateProjectFlavorCfg(IVsCfg pBaseProjectCfg, out IVsProjectFlavorCfg ppFlavorCfg)
So here I implemented a static mapping between my custom IVsProjectFlavorCfg and the specified IVsCfg
Already having a EnvDTE.Project reference, I could then use the following to get a IVsCfg reference:
IVsHierarchy hierarchy1 = null;
var sol = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution;
sol.GetProjectOfUniqueName(project.UniqueName, out hierarchy1);
IVsSolutionBuildManager bm = Package.GetGlobalService(typeof(IVsSolutionBuildManager)) as IVsSolutionBuildManager;
IVsProjectCfg[] cfgs = new IVsProjectCfg[1];
bm.FindActiveProjectCfg(IntPtr.Zero, IntPtr.Zero, hierarchy1, cfgs);
IVsCfg cfg = cfgs[0] as IVsCfg;
I could then use the IVsCfg reference to look up my custom configuration provider.
If you can access the project node instance (and if your project system is based on MPF), you can just use the GetProjectProperty method of the ProjectNode class. It obtains a ProjectPropertyInstance and returns its evaluated value, or null if the property does not exist.

How to create dynamic assets in Meteor

I thought this would be easy.
I want to create simple files the user can download by clicking a link.
Write what you want into the servers assets/app folder and then generate a simple link
Download> me!
Writing files into Meteor's server side asset folder is easy. And the download link above will always download a file with the name you specified.
You will get a yourNewFile.txt in the client's download folder. But, unfortunately its content will not be what you wrote on the server (new.txt).
Meteor has the strange behavior of downloading its startup html page as the content if the name of your content wasn't originally in the public folder. I think this is bug .... put the above anchor into a default Meteor project and click the link .. don't even create a public folder. You get a downloaded file with the name you asked for...
So, if you put stubs in the public folder (you know the names of the assets you are going to create) then you can create them dynamically.
I don't know the names before hand. Is there any way to get Meteor to 'update' its assets list with the new names I want to use?
I know there are packages that can do this. I'd like to just do it myself as above, really shouldn't be this hard.
The public/ folder intended use is specifically for static assets. Its content is served by the node http server.
If you want to dynamically generate assets on the server, you can rely on iron:router server side routes.
Here is a simple example :
lib/router.js
Router.route("/dynamic-asset/:filename",function(){
var filename = this.params.filename;
this.response.setHeader("Content-Disposition",
"attachment; filename=" + filename);
this.response.end("Hello World !");
},{
name: "dynamic-asset",
where: "server"
});
In server-side route controllers, you get access to this.response which is a standard node HTTP response instance to respond to the client with the correct server generated content. You can query your Mongo collections using the eventual parameters in the URL for example.
client/views/download/download.html
<template name="download">
{{#linkTo route="dynamic-asset" target="_blank" download=""}}
Download {{filename}}
{{/linkTo}}
</template>
client/views/parent/parent.html
<template name="parent">
{{> download filename="new.txt"}}
</template>
The linkTo block helper must be called in a context where the route parameters are accessible as template helpers. It will generate an anchor tag having an href set to Router.path(route, dataContext). It means that if our server-side route URL is /dynamic-asset/:filename, having a data context where filename is accessible and set to "new.txt" will generate this URL : /dynamic-asset/new.txt.
In this example we set the current data context of the download template to {filename: "new.txt"} thanks to the template invocation syntax.
Note that target="_blank" is necessary to avoid being redirected to the dynamic asset URL inside the current tab, and the download HTML attribute must be set to avoid considering the link as something the browser should open inside a new tab. The download attribute value is irrelevant as it's value will be overriden server-side.
Here is the raw Picker (meteorhacks:picker) route and method I used to get this running. I've kept it lean and its just what I got working and probably not the best way to do this ... the synchronous methods (like readFileSync) throw exceptions if things are not right, so they should be wrapped in try-catch blocks and the mkdirp is a npm package loaded through meteorhacks:npm package hence the Meteor.npmRequire. Thanks again to saimeunt for the directions.
Picker.route('/dynamic-asset/:filename', function(params, req, res, next) {
console.log('/dynamic-asset route!');
var fs = Npm.require('fs');
var path = Npm.require('path');
var theDir = path.resolve('./dynamic-asset');
var filename = params.filename;
var fileContent = fs.readFileSync(theDir + '/' + filename, {encoding:'utf8'});
res.end(fileContent);
});
The Meteor method that creates the file is
writeFile: function(fname, content) {
console.log('writeFile', fname);
var fs = Npm.require('fs');
var path = Npm.require('path');
var mkdirp = Meteor.npmRequire('mkdirp');
// verify/make directory
var theDir = path.resolve('./dynamic-asset');
mkdirp(theDir);
fs.writeFileSync(theDir + '/' + fname, content);
return 'aok';
}
and the hyper link I generate on the client if the file gets created looks like this:
Download lane file now
I incorrectly stated in my original question at the top that you could use stubs and write files into the assets folder. Its not so .. you will only get back the stub ... sorry.

push external multimedia file in to package at tridion publish time

When we publish some page/dynamic component from tridion is it possible to add some external multimedia file/content(ex:jpg image) in to current executing/rendering package at publish time.So that final transportation package has this binary file present along with original published content?
Is this achivable using customization of tridion renderer/resolver?If yes please provide some inputs.
*Note:*The binary content that needs to be pushed in to package at publish time is not present as multimedia component in tridion, it is located at other file location outside tridion CMS.Instead we have some stub multimedia component being used inside published component/page which has some dummy image. we plan to replace the stub image with original image at publish(rendering/resolving) time.
Since we have huge bulk of binary content stored in DAM tool we dont want that data to be recreated as multimedia component in tridion, insted we want to use that data by querying DAM tool and attach it in to tridion package with some logical referencesplanning to maintain one to one mapping between stub multimedia comp tcmid to original content in some mapping DB for reference).
Please let us know if any solution is there to attach external binary content to package at publish time.
The best - and easiest way - is to use the mechanism provided by Tridion out-of-the-box for this. Create a new multimedia component, select "External" in the resource type drop-down, and type the URL to the object. As long as you can address it with a URL, it will work exactly as you want (item will be added to package and sent to delivery server).
If this is not good enough for you, then yes, you can add it to the package yourself. I've done this in the past with code somewhat like this:
FileInfo file = // Weird logic to get a FileInfo object from external system
Item item = package.GetItem("My original Item");
item.SetAsStream(file.OpenRead());
This replaced the content of my original component with the actual file I wanted. This will work for you IF the original component is also a multimedia component. If it's not, just create a new item with your own name, etc. If possible, do use the out-of-the-box process instead.
PS: FileInfo Class.
As Nuno suggested the best way is to use multimedia component with 'External' resource type. You may not need to create these manually, you can automate using core services or API programs.
Another way I used before to create zip file at run time and add same to package with following code. Hope it may help.
using (MemoryStream ms = new MemoryStream())
{
zip.Save(ms);
downloadAllInOneURL = String.Format("ZipAsset{0}.zip", uniqueZipID);
downloadAllInOneURL = m_Engine.PublishingContext.RenderedItem.AddBinary(ms, downloadAllInOneURL, "", "application/zip").Url;
downloadAllInOneSize = getSize(ms.Length);
}

ASP.Net How to access images from different applications

I have 2 different project. One is supposed to upload images (admin) and the other is supposed to show them.
I was writing something like "/Contents/images/image path"... But wait! I will I upload the images from the application into that address?
Any help and suggestions please.
If you have two applications that will interact with the same files, it's probably better to have an ImageController with an action that allows you to upload/download the image rather than storing them directly as content. That way both applications can reference the same file location or images stored in a database and manipulate them. Your download action would simply use a FileContentResult to deliver the bytes from the file. You can derive the content type from the file extension.
Example using a database. Note that I assume that the database table contains the content type as determined at upload time. You could also use a hybrid approach that stores the image metadata in a database and loads the actual file from a file store.
public class ImageController : Controller
{
public ActionResult Get( int id )
{
var context = new MyDataContext();
var image = context.Images.SingleOrDefault( i => i.ID == id );
if (image != null)
{
return File( image.Content, image.ContentType );
}
// or you could return a placeholder image here if appropriate.
throw new HttpException( 404, "The image does not exist" );
}
}
An alternative would be to incorporate your administrative interface in an area of the same application rather than in a separate project. This way you could reuse the content/images directory if you wanted. I find that when you have dynamic images the database or a hybrid approach works better from a programming perspective since it's more consistent with the rest of your data model.
you could try like this..
Let's assume that all of your images are in Project A and you want to use the same images in Project B.
Open Project B with Visual Studio. In the Solution Explorer, right click your Project Name and select "Add Existing Item...".
Browse to the physical location on disc where your images in Project A are stored and select the files that you want to import.
You'll then be able to access those images from project A in Project B.

How can I associate many existing files with drupal filefield?

I have many mp3 files stored on my server already from a static website, and we're now moving to drupal. I'm going to create a node for each audio file, but I don't want to have to upload each file again. I'd rather copy the files into the drupal files directory where I want them, and then associate the nodes with the appropriate file.
Any ideas on how to accomplish that?
Thanks!
I am not sure if I am going to propose a different approach or if I am about to tell with different words what you already meant with your original question, but as you want the nodes to be the files, I would rather generate the nodes starting from the files, rather than associating existing nodes with existing files.
In generic terms I would do it programmatically: for each existing files in your import directory I would build the $node object and then invoke node_save($node) to store it in Drupal.
Of course in building the $node object you will need to invoke the API function of the module you are using to manage the files. Here's some sample code I wrote to do a similar task. In this scenario I was attaching a product sheet to a product (a node with additional fields), so...
field_sheet was the CCK field for the product sheet in the product node
product was the node type
$sheet_file was the complete reference (path + filename) to the product sheet file.
So the example:
// Load the CCK field
$field = content_fields('field_sheet', 'product');
// Load the appropriate validators
$validators = array_merge(filefield_widget_upload_validators($field));
// Where do we store the files?
$files_path = filefield_widget_file_path($field);
// Create the file object
$file = field_file_save_file($sheet_file, $validators, $files_path);
// Apply the file to the field, this sets the first file only, could be looped
// if there were more files
$node->field_scheda = array(0 => $file);
// The file has been copied in the appropriate directory, so it can be
// removed from the import directory
unlink($sheet_file);
BTW: if you use a library to read MP3 metadata, you could set $node->title and other attributes in a sensible way.
Hope this helps!
The file_import module doesn't do exactly what you want (it creates node attachments instead of nodes), but it would be relatively simple to use that module as guidance along with the Drapal API to do what you want.

Resources