Alfresco: Search all document in all workflows - java

I need to search for all documents in all workflow.
The document in workflow contains anything property, that indicates that the document is in some workflows?
Example:
results = search.luceneSearch("#cm\\:documentWorkflow:"+true);
I need create custom advanced search and search all documents in workflows.
Thanks in advance.

If it is a simple workflow there is an aspect called "{http://www.alfresco.org/model/application/1.0}simpleworkflow"
If you are an advanced workflow, have a parent type "packageContains" containing workflow in this node (parent) one aspect "{http://www.alfresco.org/model/bpm/1.0}workflowPackage"
You could perform a query something like:
search.luceneSearch results = ('ASPECT:"bpm:workflowPackage" ASPECT:"app:simpleWorkflow"');
Where node results contain this aspect, the children are documents.
For example:
var res = search
.luceneSearch('ASPECT:"bpm:workflowPackage" ASPECT:"app:simpleWorkflow"');
var par = null;
var c = null;
var s = "<html><body>total " + res.length + "<br>";
for (var i = 0; i < res.length; i++) {
if (res[i].hasAspect("bpm:workflowPackage")) {
par = res[i];
for each(c in par.children)
{
s += c.name + "<br>";
}
} else {
s += res[i].name + "<br>";
}
}
s += "</body></html>";
s;
Regards!

Related

Parsing currency exchange data from https://uzmanpara.milliyet.com.tr/doviz-kurlari/

I prepare the program and I wrote this code with helping but the first 10 times it works then it gives me NULL values,
String url = "https://uzmanpara.milliyet.com.tr/doviz-kurlari/";
//Document doc = Jsoup.parse(url);
Document doc = null;
try {
doc = Jsoup.connect(url).timeout(6000).get();
} catch (IOException ex) {
Logger.getLogger(den3.class.getName()).log(Level.SEVERE, null, ex);
}
int i = 0;
String[] currencyStr = new String[11];
String[] buyStr = new String[11];
String[] sellStr = new String[11];
Elements elements = doc.select(".borsaMain > div:nth-child(2) > div:nth-child(1) > table.table-markets");
for (Element element : elements) {
Elements curreny = element.parent().select("td:nth-child(2)");
Elements buy = element.parent().select("td:nth-child(3)");
Elements sell = element.parent().select("td:nth-child(4)");
System.out.println(i);
currencyStr[i] = curreny.text();
buyStr[i] = buy.text();
sellStr[i] = sell.text();
System.out.println(String.format("%s [buy=%s, sell=%s]",
curreny.text(), buy.text(), sell.text()));
i++;
}
for(i = 0; i < 11; i++){
System.out.println("currency: " + currencyStr[i]);
System.out.println("buy: " + buyStr[i]);
System.out.println("sell: " + sellStr[i]);
}
here is the code, I guess it is a connection problem but I could not solve it I use Netbeans, Do I have to change the connection properties of Netbeans or should I have to add something more in the code
can you help me?
There's nothing wrong with the connection. Your query simply doesn't match the page structure.
Somewhere on your page, there's an element with class borsaMain, that has a direct child with class detL. And then somewhere in the descendants tree of detL, there is your table. You can write this as the following CSS element selector query:
.borsaMain > .detL table
There will be two tables in the result, but I suspect you are looking for the first one.
So basically, you want something like:
Element table = doc.selectFirst(".borsaMain > .detL table");
for (Element row : table.select("tr:has(td)")) {
// your existing loop code
}

Problem loading a XML file using Dom parser

I am new with Java programming and I have problem reading XML-file. I am trying to save information from XML using DOM parser. I load the xml into a Document and then trying to save all the schedules of a radio channel in a NodeList. but the program saves repeatedly just infromation of the first node. Where is the problem with my code ?
NodeList episodeElement = doc.getElementsByTagName("schedule");
for (int i = 0; i < episodeElement.getLength(); i++) {
Node n = episodeElement.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE && getSize(doc) != 0) {
Element e = (Element) n;
String title = e.getElementsByTagName("title").item(i).getTextContent();
NodeList nd = e.getElementsByTagName("description");
String description;
if (nd.getLength() > 0) {
description = nd.item(i).getTextContent();
}else {
description = null;
}
String startTime = e.getElementsByTagName("starttimeutc").item(i).getTextContent();
String endTime = e.getElementsByTagName("endtimeutc").item(i).getTextContent();
Program prog = new Program(id, title, description, startTime, endTime);
System.out.println(startTime);
programs.add(i, prog);
}
else {
System.out.println("No schedules found");
}
}
You haven't used .getChildNodes() method to traverse a layer down the tag and looped around that, that's why it is just fetching you information of the first node.
Visit this link and u can find an excellent example.
https://www.youtube.com/watch?v=HfGWVy-eMRc

convert XML to a custom Excel with Java

I need advise about how to convert XML to a custom Excel with Java
I need to convert XML to Excel with a custom layout. I found a POI and it seems like it can help with this task. But I don't have this experiences and as I understood POI works the best with in memory trees like DOM. I started to pars my XML(I can show a small part of, it's really big and goes deep)
<advantage>
<companies>
<name>Name1</name>
<name>Name2</name>
<name>Name3</name>
<name>Name4</name>
<name>Name6</name>
</companies>
<companyPreCode>
<PreCode>1</PreCode>
<PreCode>2</PreCode>
<PreCode>3</PreCode>
<PreCode>4</PreCode>
<PreCode>6</PreCode>
</companyPreCode>
by using DOM as I saw in one online tutorial like this
Document xmlDoc = getDocument("./src/xmlForTest.xml");
xmlDoc.getDocumentElement().normalize();
System.out.println("Root element of the doc is :\" "+ xmlDoc.getDocumentElement().getNodeName() + "\"");
NodeList listOfAdvantage = xmlDoc.getElementsByTagName("advantage"); //first we need to find total number of Advantage blocks
int totalAdvantage = listOfAdvantage.getLength();
System.out.println("Total no of advantage : " + totalAdvantage);
for (int s = 0; s < listOfAdvantage.getLength(); s++) //get into advantage
{
Node AdvantageNode = listOfAdvantage.item(s);
System.out.println("advantage number : " + s);
if (AdvantageNode.getNodeType() == Node.ELEMENT_NODE)
{
Element AdvantageElement = (Element) AdvantageNode;
NodeList CompanyList = AdvantageElement.getElementsByTagName("companies"); // find node companies
System.out.println("companies number : " + CompanyList.getLength());
for(int cl = 0; cl < CompanyList.getLength(); cl++) {
NodeList CompanyNameList = CompanyList.item(cl).getChildNodes(); //AdvantageElement.getElementsByTagName("name");
for (int j = 0; j < CompanyNameList.getLength(); j++) {
Node childNode = CompanyNameList.item(j);
if ("name".equals(childNode.getNodeName())) {
for (int nl = 0; nl < CompanyNameList.getLength(); nl++) {
Element CompanyNameElement = (Element) CompanyNameList.item(nl);
NodeList textFNList = CompanyNameElement.getChildNodes();
System.out.println("Company: " + nl + " :" + (textFNList.item(0)).getNodeValue().trim());
CompaniesNames.add((textFNList.item(0)).getNodeValue().trim());
}
}
}
}
}// end of if clause
}// end of for loop with s var
and now I have several questions
How to make this parsing easier? my file is big and in some places I Have the same tags for different things, like Name can be for company, product or a person. But it's getting hard to retrieve it one by one the way I did it
How to feed this data later into POI so I can start using this POI to build my Excel files? Because right now I have a set of ArrayLists with my data from different tags and I just don't know what I need to next with it

How to generate XPath query matching a specific element in Jsoup?

_ Hi , this is my web page :
<html>
<head>
</head>
<body>
<div> text div 1</div>
<div>
<span>text of first span </span>
<span>text of second span </span>
</div>
<div> text div 3 </div>
</body>
</html>
I'm using jsoup to parse it , and then browse all elements inside the page and get their paths :
Document doc = Jsoup.parse(new File("C:\\Users\\HC\\Desktop\\dataset\\index.html"), "UTF-8");
Elements elements = doc.body().select("*");
ArrayList all = new ArrayList();
for (Element element : elements) {
if (!element.ownText().isEmpty()) {
StringBuilder path = new StringBuilder(element.nodeName());
String value = element.ownText();
Elements p_el = element.parents();
for (Element el : p_el) {
path.insert(0, el.nodeName() + '/');
}
all.add(path + " = " + value + "\n");
System.out.println(path +" = "+ value);
}
}
return all;
my code give me this result :
html/body/div = text div 1
html/body/div/span = text of first span
html/body/div/span = text of second span
html/body/div = text div 3
in fact i want get result like this :
html/body/div[1] = text div 1
html/body/div[2]/span[1] = text of first span
html/body/div[2]/span[2] = text of second span
html/body/div[3] = text div 3
please could any one give me idea how to get reach this result :) . thanks in advance.
As asked here a idea.
Even if I'm quite sure that there better solutions to get the xpath for a given node. For example use xslt as in the answer to "Generate/get xpath from XML node java".
Here the possible solution based on your current attempt.
For each (parent) element check if there are more than one element with this name.
Pseudo code: if ( count (el.select('../' + el.nodeName() ) > 1)
If true count the preceding-sibling:: with same name and add 1.
count (el.select('preceding-sibling::' + el.nodeName() ) +1
This is my solution to this problem:
StringBuilder absPath=new StringBuilder();
Elements parents = htmlElement.parents();
for (int j = parents.size()-1; j >= 0; j--) {
Element element = parents.get(j);
absPath.append("/");
absPath.append(element.tagName());
absPath.append("[");
absPath.append(element.siblingIndex());
absPath.append("]");
}
This would be easier, if you traversed the document from the root to the leafs instead of the other way round. This way you can easily group the elements by tag-name and handle multiple occurences accordingly. Here is a recursive approach:
private final List<String> path = new ArrayList<>();
private final List<String> all = new ArrayList<>();
public List<String> getAll() {
return Collections.unmodifiableList(all);
}
public void parse(Document doc) {
path.clear();
all.clear();
parse(doc.children());
}
private void parse(List<Element> elements) {
if (elements.isEmpty()) {
return;
}
Map<String, List<Element>> grouped = elements.stream().collect(Collectors.groupingBy(Element::tagName));
for (Map.Entry<String, List<Element>> entry : grouped.entrySet()) {
List<Element> list = entry.getValue();
String key = entry.getKey();
if (list.size() > 1) {
int index = 1;
// use paths with index
key += "[";
for (Element e : list) {
path.add(key + (index++) + "]");
handleElement(e);
path.remove(path.size() - 1);
}
} else {
// use paths without index
path.add(key);
handleElement(list.get(0));
path.remove(path.size() - 1);
}
}
}
private void handleElement(Element e) {
String value = e.ownText();
if (!value.isEmpty()) {
// add entry
all.add(path.stream().collect(Collectors.joining("/")) + " = " + value);
}
// process children of element
parse(e.children());
}
Here is the solution in Kotlin. It's correct, and it works. The other answers are wrong and caused me hours of lost work.
fun Element.xpath(): String = buildString {
val parents = parents()
for (j in (parents.size - 1) downTo 0) {
val parent = parents[j]
append("/*[")
append(parent.siblingIndex() + 1)
append(']')
}
append("/*[")
append(siblingIndex() + 1)
append(']')
}

get a property value of an individual jena api

I have my class Alert which contain as individual
Alert_1
Alert_2
Alert_3
and each individual has properties witch contains values
for example
Alert_1 :
hasanalyser : analyser546
hastime: 10
hasdatainfo: difficult
I can now get all individuals but I can not get those (hasanalyser, hastime and hasdatainfo) values
Here is my code and it works. How I can get what I want please?
owlModel = ProtegeOWL.createJenaOWLModelFromURI("file:///D:/base_connaissance.owl");
OntModel model = owlModel.getOntModel();
OWLNamedClass theAlert = owlModel.getOWLNamedClass("Alert");
Collection CAlerte = theAlert.getInstances();
int nombreAlerte =CAlerte.size();
String[ ] list_alerte=new String[ nombreAlerte ];
OWLIndividual[ ] idorg=(OWLIndividual[ ]) CAlerte.toArray(new OWLIndividual[ 0 ]);
for(int j=0;j< nombreAlerte;j++){
list_alerte[ j ]=idorg[ j ].getName();
}
System.out.println(" le nombres des alerte est:"+nombreAlerte);
OntModel inf1 = ModelFactory.createOntologyModel();
for(int k=0;k< nombreAlerte;k++){
System.out.println(" \n"+list_alerte[k]);
}
Here it display my
Alert_1
Alert_2
Alert_3
How to get their properties?
UPDATE:
Thanks for your answer, it doesn't work yet. I tried now to do like you said
JenaOWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI("file:///D:/base_connaissance.owl");
OntModel model = owlModel.getOntModel();
ArrayList<Resource> results = new ArrayList<Resource>();
ExtendedIterator individuals = model.listIndividuals();
while (individuals.hasNext()) {
Resource individual = (Resource) individuals.next();
results.add(individual);
}
for(int i = 0; i < results.size(); i++)
{
System.out.println("individual number " + i + " = " + results.get(i));//here it display my individual
Individual ind = model.getIndividual(results.get(i).toString());
Property hasTime = model.createProperty( "file:///D:/base_connaissance.owl#hasanalyser" );
RDFNode time = ind.getPropertyValue( hasTime );
System.out.println("property value of hasanalyser "+time);
At the end it displays all names of my individuals, and after each individual it display property value of hasanalyser NULL.
where is the problème please
IT WORK NOW , i can get now all properties of all individuals thanks lot , now what it doesnt work is HOW to add an individual and add properties to this individual and insert it into my "base_connaissance.owl" if you can help me i realy appreciate it here is the full code witch work perfectly .
static JenaOWLModel owlModel ;
public static void main(String[] args) {
OntModel model;
javax.swing.JDialog jDialog1 = new javax.swing.JDialog();
try{
String ns="file:///D:/base_connaissance.owl#";
owlModel = ProtegeOWL.createJenaOWLModelFromURI("file:///D:/base_connaissance.owl");// crée un modele owl a partir du ficher owl charger
model = owlModel.getOntModel();
JOptionPane.showMessageDialog(jDialog1,"chargement du fichier effectuer avec succé","Information",JOptionPane.INFORMATION_MESSAGE);
ArrayList<Resource> results = new ArrayList<Resource>();
ExtendedIterator individuals = model.listIndividuals();
while (individuals.hasNext()) {
Resource individual = (Resource) individuals.next();
results.add(individual);
}
System.out.println("\n");
for(int i = 0; i < results.size(); i++)
{
Individual ind = model.getIndividual(results.get(i).toString());
System.out.println(""+ind);
StmtIterator it = ind.listProperties();
while ( it.hasNext()) {
Statement s = (Statement) it.next();
if (s.getObject().isLiteral()) {
System.out.println(""+s.getLiteral().getLexicalForm().toString()+" type = "+s.getPredicate().getLocalName());
}
else System.out.println(""+s.getObject().toString().substring(53)+" type = "+s.getPredicate().getLocalName());
}
System.out.println("\n");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(jDialog1,
"error",
"Information",
JOptionPane.INFORMATION_MESSAGE);
}
}
I'm not clear from your code how you are using Jena models. The OntModel called model does not appear to be used, nor does the InfModel called inf1. OWLIndividual is not a Jena class, but a Protégé one. The question asks about using the Jena API, however, so the rest of this answer assumes that you can get the Individuals that you are interested in, not the OWLIndividuals.
Jena's Individuals have a method getPropertyValue that returns the value of a property for the individual. You should be able to do something like this:
Individual alert1 = ...;
String hasTimeURI = "...";
Property hasTime = model.createProperty( hasTimeURI );
RDFNode time = alert1.getPropertyValue( hasTime );
The additional code you posted is printing null probably because the IRI for the property is incorrect. File-based IRIs are not very good identifiers; URIs are supposed to be universal identifiers, and something that includes a file path almost certainly isn't going to be universal. Without seeing your data, we cannot know what the proper IRI for the property should be. However, if you do something like this, you should be able to get enough information to determine what the property IRI should be.
Individual ind = model.getIndividual(results.get(i).toString());
// Check whether ind is null.
System.out.println( "ind: "+ind );
// Iterate over all the statements that have ind as subject.
StmtIterator it = ind.ListProperties();
while ( it.hasNext() ) {
System.out.println( it.next() );
}

Categories