Fetch all the values of specific property in aem using queryBuilder - aem

I am having scenario in which i want to fetch all the values of a property under a specific path in AEM using QueryBuilder api.
This property can have single or multivalued.
Any help will be appreciated!!

Example that can help you. is below as it is just for illustration written in simple JSP scriptlets
<%
Iterator<Resource> iter = resourceResolver.findResources("/jcr:root/content/geometrixx-outdoors//element(*, nt:unstructured)[(#imageRotate = '0' or #imageRotate = '1')]","xpath");
while (iter.hasNext()) {
Resource child = iter.next();
out.println("</br>"+child.getPath());
Node node = child.adaptTo(Node.class);
Property nProp = node.getProperty("imageRotate");
if(nProp.isMultiple()) // This condition checks for properties whose type is String[](String array)
{
Value[] values = nProp.getValues();
out.println(" :: This is a multi valued property ::");
for (Value v : values) {
out.println("</br>"+"Property Name = "+nProp.getName()+" ; Property Value= "+v.getString());
}
}
else if(!nProp.getDefinition().isMultiple()){
out.println("</br>"+"Property Name = "+nProp.getName()+" ; Property Value= "+nProp.getString());
}
}
%>
Here i have used the Iterator<Resource> iter = resourceResolver.findResources(query,"xpath"); which can give you the query results which matches the imageRotate property under /content/geometrixx-outdoors/ path which consists combination of single and multivalued as shown in below screenshot.

There is no direct way to fetch the properties using query builder api. I would suggest you to create a servlet resource, which requires a path and property name.
Fetch the jcr node using the given path via QueryBuilder. Then, you need to loop through the results to check the property of the nodes. You can access the multiple property values, once you have a node.

Related

How can I dynamically change the default value for views exposed filter referencing taxonomy terms when the term weight value = 0

I have an exposed filter in a view is referencing taxonomy terms from specific vocabulary. The operator (screenshot below) allows selecting a term as the default value for that filter. What I want to achieve is to dynamically select the default value when the term weight = 0.
This should allow less privileged roles like Content Contributors to set the default value for the exposed filter by changing the order (weight) of the term without the need to edit the views settings.
I tried to research this and so far, it seems the best way to achieve this is by using (hook_views_pre_build) but I just don't know how.
After extensive search and experiment, this code worked for me:
use Drupal\taxonomy\Entity\Term;
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_pre_build().
*/
function YOUR_MODULE_NAME_views_pre_build(ViewExecutable $view) {
if ($view->id() == 'YOUR_VIEW_ID' && $view->current_display == 'YOUR_DISPLAY_ID') {
// Get the exposed filter for the taxonomy terms.
$filter = $view->display_handler->getHandler('filter', 'YOUR_FILTER_ID');
if ($filter) {
// Query the database for taxonomy terms with a weight of 0.
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('weight', 0);
$term_ids = $query->execute();
if (!empty($term_ids)) {
// Load the first term found with a weight of 0.
$term = Term::load(array_shift($term_ids));
if ($term) {
// Set the default value for the exposed filter to the term's ID.
$filter->options['value'] = $term->id();
}
}
}
}
}
This code will query the database for taxonomy terms with a weight of 0 when the specified view is pre-built and set the default value for the exposed filter to the ID of the first term it finds. If no terms are found with a weight of 0, it will not change the default value.

How to change the value of a tables attribute using LINQ/LAMBDA?

I have an order table and an orderStatus table, their relationship is as seen below:
I want to be able to change 'StatusID' to the value 2, of a specific order (i am able to get the specific order ID, and have loaded it into an integer variable) using a lambda expression within an action result - would there be any easy way of doing this?
So far i have tried:
//get specific order ID
int currentOrderId = newConfirmedOrderLine.OrderID;
//-----
Order statusChange = new Order();
statusChange.OrderStatus.StatusID = 2;
DBAccessor.SaveChanges();
I am new to linq and lambda, so any explanation with an answer would be greatly appreciated!
If DBAccessor is a DbContext then this could/should work. You need to load the Order entity that you want to change from the DBAccessor.Order DbSet, change it by setting a property, and then call SaveChanges.
var orderStatus = DBAccessor.OrderStatus.First(x => x.StatusID == 2);
var order = DBAccessor.Order.Find(currentOrderId);
order.OrderStatus = orderStatus;
DBAccessor.SaveChanges();

Extbase mapOnProperty with inline field

I have an extension with an extbase model, which I want to extend. Works fine for all fields, except an "inline" field. When I map that inline field, I do not receive all child elements, but only one child element with the counter as uid.
Example: The inline field "description" has three children (uid = 17, uid = 18, uid = 19), so the field tx_firstextension_domain_model_job.description contains the number 3. In the frontend, I have an ObjectStorage, containing exactly one Description model (uid = 3)
Here is my TypoScript:
config.tx_extbase {
objects.Foo\FirstExtension\Domain\Model\Job.className = Bar\SecondExtension\Domain\Model\Job
persistence.classes.Bar\SecondExtension\Domain\Model\Job.mapping {
table = tx_firstextension_domain_model_job
columns {
description.mapOnProperty = description
anyotherfield.mapOnProperty = anyotherfield
onemorefield.mapOnProperty = onemorefield
}
}
}
SOLUTION
My problem was a wrong configuration option. Correct is tableName, but I used table. Here is the corrected - end even more simple - snippet:
config.tx_extbase {
objects.Foo\FirstExtension\Domain\Model\Job.className = Bar\SecondExtension\Domain\Model\Job
persistence.classes.Bar\SecondExtension\Domain\Model\Job.mapping {
tableName = tx_firstextension_domain_model_job
}
}
There seems to be a misconfiguration in your Job-Models configuration. Please check the following:
Is inside the model (Job.php) the property description configured as ObjectStorage (including getter and setter function + annotations correctly) and does the property be instantiated as ObjectStorage in the __construct function?
Is the field correctly configured as type "inline" in the TCA configuration file Configuration/TCA/tx_secondextension_domain_model_job.php
Probably you have to extend the TCA of the first extension in order to make inline work when extending other extensions. Then you maybe have to add the field description to Configuration/TCA/Overrides/tx_firstextension_domain_model_job.php but I'm not sure if this is really necessary...

GWT get Elements by attribute

Which way I can get GWT Elements by it's attribute ? I would like to get all of Elements by giving specific attribute and attribute-value.
<input type = "text" class = "get-TextBox" nature = "price"/>
<input type = "text" class = "get-TextBox" nature = "address"/>
<input type = "text" class = "get-TextBox" nature = "price"/>
above snipet , I want to get input elements by "nature" attribute and containing attribute-values of "price".
At my actual problem ,they are created by dynamic and not in same panel.Many Elements these having "nature" attribute and "price" attribute value. So , I am using DOM Handler for them.
Eg: JQuery (get element by attribute) , it is my main point to get.
But please don't suggest to ( iterate and check it's attribute ) . I am finding the easiest way to get it.
I would really appreciate your suggestions !
You can use this code:
NodeList<Element> elements = Document.get().getElementsByTagName("input");
to get all of your inputs. Next step should be to iterate over the list and check the attributes with:
for (int i = 0; i < elements.getLength(); i++) {
if (elements.getItem(i).getAttribute("nature").equals("price")) {
// found it
}
}
a better way would be to use the attribute id instead of nature. In this case you get the element with:
Document.get().getElementById("price")
Alternative you can use Errai where you can bind your widgets to native HTML.

Richfaces 4 dynamic select options when user type

I am using rich faces select component.
I want dynamic values when user manually type some thing in the select component.
<rich:select enableManualInput="true" defaultLabel="start typing for select" value="#{supplierSearchBean.userInput}">
<a4j:ajax event="keyup" execute="#this" listener="#{supplierSearchBean.userInputChange}"/>
<f:selectItems value="#{supplierSearchBean.selectOptions}" />
</rich:select>
Java code as follows
public void userInputChange(ActionEvent ae){
Map map = ae.getComponent().getAttributes();
System.out.println(map.toString());
}
public void setUserInput(String userInput) {
System.out.println("userINput = " + userInput);
this.userInput = userInput;
}
Here i found 2 issues
1st: setUserINput always print empty string when user type value
2nd: listener method never get call.
any help ?
The problem is most probably that there is no selected value while the user types, and this component restricts the allowed values to the specified select items. A partial input is thus not valid and cannot be bound to your bean.
I think you could get the expected behavior if you use a rich:autocomplete instead. However, if you want to restrict the allowed values, maybe you can keep your rich:select and listen for the selectitem event.
Override getItems function in richfaces-utils.js file in richfaces-core-impl-4.0.0.Final.jar under richfaces-core-impl-4.0.0.Final\META-INF\resources folder.
Change the condition of pushing items to be
if(p != -1)
instead of
if(p == 0)
This should fix the issue.