I have created a js file to create folders when creating an event in Alfresco. But the problem is, when I create an event on the Alfresco site, it will automatically create a folder in a shared folder. That's a problem because when updating an existing event it creates a new folder without removing the existing folder. Also, when I delete an event it will not delete the corresponding folder.
How can I solve this problem?
Here is my code to create a new folder when an item is created:
// create a new folder in the same space
var folderNode = space.createFolder(document.properties["ia:whatEvent"]);
// copy the doc into the newly created folder node
//var copy = document.copy(folderNode);
// move the folder node to companyhome
var objDestFolder = companyhome.childByNamePath("Shared/SECRETARY/COMMISSION_PAPER_RECEIVED");
folderNode.move(objDestFolder);
If you wanted to create folder in the site than you need to specify the parent folder object where the new folder/document will get created.
like docLibContainer.createFolder for this docLibContainer is parent folder object.
In your case space is parent folder object.
Please try in this example docLibContainer will create a folder of name document.properties["ia:whatEvent"] in Document Library Folder.
var site = document.getSiteShortName();
var docLibContainer = siteService.getSite(site).getContainer("documentLibrary");
if(docLibContainer){
// create a new folder in the same space
var folderNode = docLibContainer.createFolder(document.properties["ia:whatEvent"]);
// copy the doc into the newly created folder node
//var copy = document.copy(folderNode);
// move the folder node to companyhome
var objDestFolder = companyhome.childByNamePath("Shared/SECRETARY/COMMISSION_PAPER_RECEIVED");
folderNode.move(objDestFolder);
}else{
// create a new folder in the same space
var folderNode = space.createFolder(document.properties["ia:whatEvent"]);
// copy the doc into the newly created folder node
//var copy = document.copy(folderNode);
// move the folder node to companyhome
var objDestFolder = companyhome.childByNamePath("Shared/SECRETARY/COMMISSION_PAPER_RECEIVED");
folderNode.move(objDestFolder);
}
Related
This might seem a little convoluted, so I apologize in advance if it does.
I'm attempting to create a build tool in Node.js for Chrome Extensions, so the final JS file has to be ES5 compliant.
I have separate JS files for each setting within the extension that export an object, the function within this object needs to be imported into the contentScript.js file and placed within it's own settings object.
I've put together a basic screenshot that shows how this should flow, but I'm not sure of how to approach this issue. I've considered some type of string interpolation within the contentScript which Node would then replace, but that seems more of a workaround than a solution.
Say we have a folder called settings, within those settings are two JavaScript files, each with a different name, type, and function, they'd look like this.
// settingName1.js
module.exports = {
name: 'Setting Name 1',
type: 'switch',
function: () => {
console.log('Setting 1 initialized');
}
}
Ideally, both of these files would have their respective functions imported into the contentScript, under a settings object, for example.
// contentScript.js
// Settings Functions Go Here
const settings = {
settingName1: function() {
console.log('Setting 1 initialized')
},
settingName2: function() {
console.log('Setting 2 initialized')
}
}
});
Basically cutting/copying the function from the source setting file itself, and pasting it under a function (named using the file's name) within the contentScript's settings object.
Here's an idea for the generated file:
// header on the file
const capturedExports = [];
// insert this prologue before each inserted file
(function() {
// =============================
// insert settingName1.js here
module.exports = {
name: 'Setting Name 1',
type: 'switch',
function: () => {
console.log('Setting 1 initialized');
}
}
// =============================
// insert this epilogue after each inserted file
})();
capturedExports.push(module.exports);
// insert this prologue before each inserted file
(function() {
// =============================
// insert settingName2.js here
module.exports = {
name: 'Setting Name 2',
type: 'switch',
function: () => {
console.log('Setting 2 initialized');
}
}
// =============================
// insert this epilogue after each inserted file
})();
capturedExports.push(module.exports);
// insert code that builds the settings object
const settings = {};
for (let exportItem of capturedExports) {
let name = exportItem.name.replace(/\s/, "");
name = name.slice(0, 1).toLowerCase() + name.slice(1);
settings[name] = exportItem.function;
}
You do the following steps to output a new file that is collection of all the settingNamex.js files.
Create the new file.
Write a header to it with the const capturedExports = []; line and the start of the IIFE.
For each settingNameX.js file, write that file to it.
Then write the close of the IIFE and the capturedExports.push(module.exports); after it. This will grab whatever the previous code assigned to module.exports and add it to the caputuredExports array.
Repeat this process for each settingNameX.js file.
Then, insert the code that builds the settings object from the capturedExports array.
Enclosing each inserted module in its own IIFE, gives it its own scope so it doesn't create symbol conflicts with the other inserted modules.
This makes the following assumptions.
It assumes that the code from each of the settingNameX.js files assigns the appropriate object to module.exports like your examples show. In the real world, you probably want to add a bunch of tests to see if the right thing is assigned to module.exports (proper defensive coding).
It assumes that the inserted modules are not assigning conflicting things to the global object.
It assumes your own module doesn't need the module.exports or can overwrite it at the end after the inserted modules have used it. If this assumption isn't OK, then you'd have to manually change module.exports = to something else that you define in your master module.
how can I exports this dynamic module?
// ./data/example5.js
module.exports = {title : example5, recentCheck : "2018-08-22"}
The contents change in real time. And I execute the followed function once a minute and connect the module.
var files = fs.readdirSync(`./data/`);
var i = 0;
var link = [];
while(i<files.length){ // read all file
link[i] = require(`../data/${files[i]}`);
//main.js
setInterval(start,10000);
I try to create and connect a new module file once a minute, but the first stored file(module) is extracted. The modulefile is being saved in real time correctly.
Shutting down and running the node will extract the changed modules correctly.
How do I handle a dynamically changing module?
I would recommend saving the data in a JSON file and then reading the data from the file rather than trying to use it as a module.
Just make the objects you're updating variables in the module that you're including.
Make a function called getVariable, and simply return the variable.
Include getVariable in your main module.
I have a C# application which launches an Electron app (node.js). I'm attempting to pass a command line argument to the Node.JS application but when I access process.argv from within index.js the argument isn't there. Is there anything specific I should be doing to retrieve this argument from within my node application?
Process process = new Process();
process.StartInfo.FileName = pathToEXE;
process.StartInfo.Arguments = argument;
process.EnableRaisingEvents = true;
process.Start();
Basically process.argv array returns 2 values which are installed location and file opening path. So first of all you should assign those values in global object in main.js as below,
global.sharedObject = { installedLocation: process.argv[0], openFilePath:
process.argv[1]}
and access this in your index.js as below,
var remote = require('electron').remote;
var location = remote.getGlobal('sharedObject').installedLocation;
var filePath = remote.getGlobal('sharedObject').openFilePath;
I have some files that need database access so I have a file like this:
...
var dynamo = new AWS.DynamoDB.DocumentClient();
module.exports.getDatabase= function(){
return dynamo;
};
...
I wonder if different .js files use it like this:
var DataUtil = require('./shared/dataUtils.js');
...
var database = DataUtil.getDatabase();
....
are they using the same instance of the object? or just instantiating a copy for each of the .js file using the requiring?
Yes, it's the same instance. When you require a module, it's only loaded when it's not already loaded. So there's only one instance of a module in a node program.
From the documentation:
Modules are cached after the first time they are loaded. This means
(among other things) that every call to require('foo') will get
exactly the same object returned, if it would resolve to the same
file.
In your case, you'll have only one instance of AWS.DynamoDB.DocumentClient.
I'm attempting to use Google Docs' ability to run scripts to create the necessary folder structure for new clients (to upload content/images into). Here's what I have thusfar:
/**
* This script creates the necessary folder structure for a new client
*/
function newClientSetup() {
var initial = DocsList.getFolder("Clients");
var client = DocsList.addtoFolder.createFolder("Client Name");
var file = DocsList.addtoFolder(client);
};
Now this is not working (TypeError: Cannot call method "createFolder" of undefined. (line 7)), but I'm having trouble figuring out how to use the Folder class within DocList. I saw that DocsList has a createFolder method, that I could use like:
var folder = DocsList.createFolder("Folder Name");
but I'm trying to start off with a parent folder, called Clients (already in Google Docs) and then create the following structure:
Clients
Client Name
Content
Images
Ideally I could run this script, but pass in a variable for Client Name to actually create the client name, but I haven't found much help from the docs. Any suggestions? Thanks!
Here is an example of how it works, see comments :
function createSubFolder(subfolder) { // the argument is the name of the folder you want to create
var parentfolder = DocsList.getFolder('Clients'); //begin in the client folder (you could also open by Id, I prefer the ID as I find it more failsafe (ID are unique, names aren't necessarily
var newFolder = DocsList.createFolder(subfolder); // create the new subfolder from the argument of the function
newFolder.addToFolder(parentfolder);// add the newly created folder to 'Clients'
}
to test this function simply use something like this :
function test(){
createSubFolder('test');// this will create a new folder called test in your Clients folder
}
note : to get the ID of your folder, take the value right behind folders/ in the url of the folder. Example in bold : https://drive.google.com/?hl=fr&tab=wo#folders/0B3qSFxxxxxxxxxdsMTFZMDQ
The sequence might be much longer if you have more folder levels... but the structure is always the same and is unique for every folder.
Here's a function and some code I wrote which might help you. It uses a sub function to see if the folder already exists and if it doesn't makes it. If the folder does already exist it returns that object which helps with chaining (referenced here on how it is used):
function newClientSetup() {
var ROOT_FOLDER = "Clients";
var CLIENTNAME_FOLDER = "Client Names";
// get a the system route folder (if it deosn't existing make it
var rootFolder = folderMakeReturn(ROOT_FOLDER);
// create/get draft and release folders
var clientNamesFolder = folderMakeReturn(CLIENTNAME_FOLDER,rootFolder, ROOT_FOLDER+"/"+CLIENTNAME_FOLDER);
}
// function to see if folder exists in DocList and returns it
// (optional - if it doesn't exist then makes it)
function folderMakeReturn(folderName,optFolder,optFolderPath){
try {
if (optFolderPath != undefined){
var folder = DocsList.getFolder(optFolderPath);
} else {
var folder = DocsList.getFolder(folderName);
}
return folder;
} catch(e) {
if (optFolder == undefined) {
var folder = DocsList.createFolder(folderName);
} else {
var folder = optFolder.createFolder(folderName);
}
return folder;
}
}
I know the question/answer is 3 years old and it was correct earlier, but this solution is no more working correctly with Google Apps script as on 2015, current easiest way to create a subfolder on a specific folder is,
function createSubFolder(parentId, folderName)
{
var id="";
try
{
var parent=DriveApp.getFolderById(parentId); // get parent folder
var folder =parent.createFolder(folderName); // create sub folder
id=folder.getId(); // get the subfolder id to return
}
catch(e)
{
}
return id;
}
pass the parent folder id with the sub folder name you want to create, the Folder class has the method createFolder to create a folder under it which returns the created subfolder's object.