groovy only grabbing first element of loop - groovy

I've got a very straightforward code snippet. That for some reason is
only grabbing the first element of the loop when I try to output it in
my jsp. JcrUtils.getChildNodes returns a NodeIterator that I thought would loop through
each property. Here is the code:
def headerNode = JcrUtils.getChildNodes(LINKS).find{
it.hasProperty("headerTitle")
it.hasProperty("headerMeta")
}
selectHeaderTitle = headerNode.getProperty("headerTitle").getString()
selectHeaderMeta = headerNode.getProperty("headerMeta").getString()
JSP:
${header.selectHeaderTitle}
${header.selectHeaderMeta}
Any help is greatly appreciated!

You want a list of Properties? You'd need findAll, also you need to && your hasProperty calls:
def headerNode = JcrUtils.getChildNodes(LINKS).findAll {
it.hasProperty("headerTitle") && it.hasProperty("headerMeta")
}

Groovy find only returns the first match.
See http://groovy.codehaus.org/Iterator+Tricks

Related

elements not getting added in list groovy script

The weird thing is happening while adding elements in list in groovy.
Scenario-
There are two List list1 and list2. List1 contains Object of X type and List2 is empty. List1 is getting populated from java file and while iterating List1 in groovy script, I am adding objects in List2.
But what happening is elements are not getting added. List2 remains empty.
If I debug the line and evaluate the expression/line it then it is getting added. But while normal debugging while executing this line, it suddenly jump to any random line.
No exception is coming.
Have created list as below:
List<X> dataToBeRemoved = new ArrayList<>()
Iterating the list as below:
for (X data in XList) {
if(something) {
dataToBeRemoved.add(data)
}
}
I am new to Groovy and If any one have ever faced this kind of issue. Please guide. Thanks.
You didn't ask, but type parameters don't get you much.
List elementsToRemove = []
And, in this case even better:
List elementsToRemove = allElements.findAll { ...some condition... }
After that, it's impossible to tell from your code. Questions such as "Why doesn't Groovy work?" are hard to answer.
You can define the an empty list by simply using
def mySmallList = []
and you may also use findAll to filter out the list
mySmallList = myBigList.findAll {//some condition }
Please check the link https://groovyconsole.appspot.com/script/5127180895911936

Create a collection of item ids Revit API in Python

So I am trying to use a list of input strings to isolate them in a view using Revit API. I got this far, but I am getting stuck where I am trying to create a set that takes all elements in a view and removes ones that are created from input IDs. I am doing this to end up with a set of all elements except ones that i want to isolate.
dataEnteringNode = IN0
view = IN0
str_ids = IN1
doc = __doc__
collector = FilteredElementCollector(doc, view.Id)
for i in str_ids:
int_id = int(i)
id = ElementId(int_id)
element = doc.GetElement(id)
element_set = ElementSet()
element_set.Insert(element)
elements_to_hide = collector.WhereElementIsNotElementType().Excluding(element_set).ToElements()
#Assign your output to the OUT variable
OUT = elements_to_hide
I would greatly appreciate a help in solving this error. I am getting that "expected ICollection[ElementId], got set". I am guessing the problem lies in a Excluding filter where i need to create a collection of Ids to exclude but I dont know how. Thank you in advance. Thank you for help in advance!
The reason your code doesn't work is that ElementSet in the Revit API does not implement the ICollection<T> interface - just the IEnumerable<T>. So, to get your code working, you will need to create an ICollection<T> object from your set.
Try something like this:
# ...
from System.Collections.Generic import List
element_collection = List[ElementId](element_set)
elements_to_hide = collector.WhereElementIsNotElementType().Excluding(element_collection).ToElements()

Groovy - XmlSlurper - find innermost element

I have the following xml:
<vehicle>
<car>
<price>100</price>
<price>200</price>
</car>
<car>
<price>300</price>
<price>400</price>
</car>
</vehicle>
Given an xml, how can we get the innermost elements (in this case, all the<price> elements)?
Assuming you have the xml in a String xml, you should be able to do:
List prices = new XmlSlurper().parseText( xml ).car.price*.text()​​
thanks Tim for the answer. I just figured out the following works too. And is more generic:
def document = slurper.parseText(xml)
def prices = document.'**'.findAll { it.children().size() == 0 }
May I suggest you next variant:
def vehicle = new XmlSlurper().parseText(xmlString)
vehicle.car.price.each {println "car's price:"+it}

Watir-WebDriver: Find elements which class is not 'completed'

I have a bunch of li elements. They are either with uncompleted, 'current' or completed class or without a class.
How do I find all li elements without completed class?
So far I am doing this by selecting necessary li objects from collection of li (through calling #attribute_value('class'), but maybe there is some elegant locating strategy in Watir-WebDriver?
UPD: As long as there is some misunderstanding of the question.
I want to know if there is locating strategy within Watir-WebDriver to find elements which class is not completed.
I know I can do this with Ruby and doing it like this:
browser.lis.select do |li|
li.attribute_value('class') != 'completed'
end
But the question is if I can do this in one line by passing some argument to #lis
UPD2: Just realized that given class names narrows solutions. Updated question and sorry for that.
The LI collection supports locators, which means you can do:
browser.lis(:class, 'uncompleted').each{ |x|
puts x.text
}
UPDATE: For the case where there are multiple classes, you can modify the above to use a regex to check for not completed:
browser.lis(:class, /^(?!completed$)/).each{ |x| puts x.class_name }
This returns all li that have no class or are not exactly 'completed' (ex 'completed2').
Note: I think .class_name may have better support than attribute_value('class') (which I believe does not work in IE as it needs to be className).
In order to not assume that there are only two classes of arrays, you can do:
all = browser.lis.collect { |li| li.class }
completed = browser.lis(:class, 'completed').collect { |li| li.class }
not_completed = (all - completed)
or even:
all = browser.lis.collect { |li| li.class }
not_completed = Array.new
all.each do |li|
if li.class != "completed"
not_completed << li
end
end

How to get the name of the current layout?

symfony getLayout does not seem to work when layout is set via view.yml. Is there anyway to get this within the controller's action class method
I recently needed this. You can do it but you just need to return the entire view.yml contents as an array:
$view_array = sfViewConfigHandler::getConfiguration(array(sfConfig::get('sf_app_config_dir').'/‌​view.yml'));
Just adjust the relative path from sf_app_config_dir (or use another marker) to get what you need.
It's not a trivial task. The view.yml, is not in the "scope" of the action.
Maybe, you can use setLayout in your action rather then in view.yml.
if you can't, for some reasons... you can try this method to reach datas in view.yml:
Is it possible to get a value from view.yml in an action
Execute the following code in the action. It works for both cases, layout set in the action or in the view.yml.
$controller = $this->getContext()->getController();
$view = $controller->getView($this->getModuleName(), $this->getActionName(), 'Success'); // viewName == 'Success' (default)
$layout_name = $view->getDecoratorTemplate(); // e.g expected: layout.php
Let us know if it works for you.

Resources