Alfresco get the path of documents using CMIS - javascript

I'm trying to fetch document objects from Alfresco Community edition,I need the documents path, while i use
SELECT * FROM cmis:document where ''
but i thing the cmis:document namespace doesn't return the path, i was wondering if there is a way to include the path.
PS : i can only use JAVASCRIPT with cmis queries

Instead of a CMIS query you should just fetch the object by URL using its object ID, then grab the path. The browser binding, which is easily invocable from JavaScript, supports this.
For example, suppose I have a file named "test-1.txt" sitting in a folder called "/test" with an Alfresco object ID of:
workspace://SpacesStore/1fb2d9cf-11ca-47c2-94b4-cf72de8f9b92
I can use this URL:
http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser/root?objectId=workspace://SpacesStore/1fb2d9cf-11ca-47c2-94b4-cf72de8f9b92&cmisselector=parents&includerelativepathsegment=true
To return JSON that includes:
{
"id": "cmis:path",
"localName": "path",
"displayName": "Path",
"queryName": "cmis:path",
"type": "string",
"cardinality": "single",
"value": "\/test"
}
Which contains the path.
You can shorten up the JSON significantly by also adding "&succinct=true"

Another option would be to write your own Javascript backed web script. It's controller would find the node using CMIS query, and it's FTL would actually display the path in any format you like.
https://community.alfresco.com/docs/DOC-6243-50-javascript-api
http://docs.alfresco.com/5.0/references/API-JS-ScriptNode.html

Related

Laravel - Modifying data for API only for convenience

Let's assume the following data that is exactly being returned like it's stored into database:
[
{
"user_name": "User 1",
"photo_file": "user1.jpg"
},
{
"user_name": "User 2",
"photo_file": "user2.jpg"
}
// ...
]
I want to use this data in a JavaScript application but I'd like to append a full path of the user's photo, like doing a treatment for the data before returning it to the API. How can I do that using Laravel?
I assume at present you're just converting the results of your query into JSON and returning that. This works, but it does mean the response is tightly coupled to your database structure, which is a bad idea. Ideally you should have a layer of abstraction to handle adding and formatting data, kind of like a view layer in MVC.
There are plenty of solutions for this. I use Fractal for my Laravel API's. It allows you to easily customise the output of a particular endpoint by specifying a transformer that will render that object for you. That way you can easily choose the data to display and format it how you wish.
Accessors are good for this.
Let's assume your data is stored in a model called Customer. I would write an accessor like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $appends = ['photo_file']; // In order to see the new attribute in json dumps
public function getPhotoPathAttribute()
{
$name = $this->getAttribute('photo_file');
if(!isset($name))
return null;
return '/full/path/to/image/' . $name;
}
}
This way you can now call $customer->photo_path and it will return `/full/path/to/image/image_name.jpg' (or null if the attribute is not set).
Edit:
In order to show this attribute in jsons (without specifically calling $model->photo_path) you will also need to add protected $appends = ['photo_file'] to the model (updated).
I would recommend against overriding original name (so I leave photo_file attribute untouched).
If you are building Laravel API, sure, as Matthew said, go and check Fractal. But don't forget to Dingo, the best tool for building API at Laravel. And it uses Fractal too.

How to get Swagger API JS function names?

I'm new to Wordnik and Swagger in general. I'm interacting with it through the Node.js JS module.
When looking at the generated Swagger UI pages, such as the sample Petstore Swagger one:
it is difficult to tell what the JavaScript functions should be when querying the server.
By JavaScript functions, I mean similar to the Petstore sample example (from the swagger-js docs):
var Swagger = require('swagger-client');
var client = new Swagger({
url: 'http://petstore.swagger.io/v2/swagger.json',
success: function() {
client.pet.getPetById({petId:7},{responseContentType: 'application/json'},function(pet){
console.log('pet', pet);
});
}
});
After connecting to swagger.json, how do they know the function to query the getPetById() function when the docs only show GET /pet/{petId}?
When using the Wordnik API, I've found it a general rule of thumb to use the get{DATATYPE}() function (with {DATATYPE} replaced with an appropriate value, of course), but the pattern has broken with getPronunciation() — it doesn't work. I don't believe the docs say it anywhere.
How could I find the JS functions for Swagger APIs?
The JavaScript method name is the same as the nickname and/or operationId field of each operation in the Swagger document, depending on which one is available.
This is an example from the Wordnik:
"path": "/word.{format}/{word}/pronunciations",
"description": "",
"operations": [
{
...
"nickname": "getTextPronunciations",
"responseClass": "List[TextPron]"
}
]
},
In this example getTextPronunciations is the JS method name.
In Wordnik, you can get the swagger.json by clicking on the Raw button on the word section of the document (next to the Expand Operations button). You can find all the other swagger.json files here: http://developer.wordnik.com/v4/

Insert object data into JSON file

I know that it's possible to read and get data of JSON file, but I didn't find any information on how to write an object to JSON file using jQuery. I understand some jQuery, but I dont have any idea how I could do this.
This is the structure of my JSON file:
{
"1": [
"6-5-2015",
"7-5-2015",
"10-5-2015"
]
}
This is an object variable that I want to write into JSON file:
var object = {"2": ["9-5-2015", "14-5-2015", "22-5-2015"]};
How can I push or insert this object to the end of my JSON file and save it, so that the JSON file could look like this?
{
"1": [
"6-5-2015",
"7-5-2015",
"10-5-2015"
],
"2": [
"9-5-2015",
"14-5-2015",
"22-5-2015"
]
}
You cannot write a file locally with Javascript, that would be a major security concern. I suggest you to move the file to your server and do a Public Api where you send the new content and write it server-side. Then request by GET the file in order to read it. Remember that you will have to lock and release the file accordingly in order to avoid loosing changes between requests.
You can't.
JavaScript does not access your disc so it can't directly write into file, so You should have some server side logic.
Reason why it can read that file because on your dist location of that file is URL. But URL on Your drive.
You cannot write a file locally but you can save cookies locally.
For managing cookies with JS you can use a plugin available here..
https://github.com/carhartl/jquery-cookie
//set
var object = {"2": ["9-5-2015", "14-5-2015", "22-5-2015"]};
$.cookie("mydata", object );
....
//get
var object = jQuery.parseJSON($.cookie('mydata'));

Apache server, JSON parser

I'm looking for json parser that can be used inside httpd.conf file in combination with url rewrite:
Here is the json file for the example:
{
"employees": [
{ "emp1":"Andy" },
{ "emp2":"David" },
]
}
For example i got this url (http://someurl.com/employees/emp1/)
employees is rewrite by the apache server to employees.json
So far so good if i just want the file, But lets say i want specific item as the url(http://someurl.com/employees/emp1/) show i want emp1:
The first thing i was looking is a way to fetch the item directly from the url for example to rewrite:
http://someurl.com/employees/emp1/
To:
http://someurl.com/employees.json:emp1/ // Just an example for a possibility that there is a syntax to fetch single item directly from the url
Another thing to consider is the use of PHP/JS to process the URL than parse and return the item (emp1 value).
So the last thing i was thinking is the use of JSON parser that can work inside the apache configuration file(httpd.conf) in combination with url rewrite process.
I'd love to hear what is the best solution, Thank you all and have a nice day.

process csv/json data in java servlets and javascript

I need an opinion on how to approach my problem. I have no idea on how to start and on how to implement which functions on which parts of the software. So this is what I want to do:
I have a Java servlet which creates a simple csv file:
name1, value1
name2, value2
etc.
This needs to be somehow converted to JSON data, so it can be displayed on a jsp page:
[
{
"name": "name1",
"value": "value1"
},
{
"name": "name2",
"value": "value2"
}
]
Then the user will be redirected to the jsp page. Is it possible to send the whole JSON structure via request object to the jsp page? Or is it the easiest if all processing is done in javascript and only the path to the csv file is sent via request object?
I'm kind of lost on this, since I first started last week with programming of web applications. I'd just need a push in the right direction and then I should be able to figure out the rest on my own ;)
First, look for a CSV parser which can turn a CSV file into a List<Bean> or List<Map<K,V>>.
Then, look for a JSON parser which can turn a List<Bean> or List<Map<K,V>> into a JSON string.
Finally, just do the math and set the resulting JSON string as a request attribute which you print in JSP as if it's a JS variable, like so <script>var data = ${data};</script>.

Categories

Resources