Orchard CMS HQL Query Taxonomies : OR in WHERE condition - orchardcms

I am trying to filter the pages that a user sees by checking for common terms between the user and the page. Both the user and the page have a Taxonomy Field of the same type ("Category"), with terms "Standard" and "Premium".
If the category of the user matches with the category of the page, I want to display it, otherwise don't. This works fine with the following IHqlQuery on PagePart:
context.Query.Where(a => a.ContentPartRecord<TermsPartRecord>()
.Property("Terms", "terms"),
x => x.InG("TermRecord.Id", ids));
where ids is the list of term ids of the user.
Apart from the above criteria, I also want to display pages that have no terms attached. This works fine on its own by:
context.Query.Where(a => a.ContentPartRecord<TermsPartRecord>(),
x => x.IsEmpty("Terms")
The problem is when I try to combine the above two conditions. I have tried:
context.Query.Where(a => a.ContentPartRecord<TermsPartRecord>()
.Property("Terms", "terms"),
p => p.Or(x => x.InG("TermRecord.Id", ids), y => y.IsEmpty("Terms")));
The second condition in the Or statement doesn't work because the query is already inside the Terms collection.
I have also tried:
context.Query.Where(a => a.ContentPartRecord<TermsPartRecord>()
p => p.Or(x => x.InG("Terms.TermRecord.Id", ids), y => y.IsEmpty("Terms")));
Here, the first condition is wrong, as I can't access an element of the Terms list with "Terms.TermRecord". This is as far as my Hql capabilities go. Do you have any suggestions? Thank you in advance.

I'm afraid I'm not all that familiar Orchard's query APIs, I always found them tough to use for more complex queries. You could always use raw HQL.
select distinct civ.Id
from Orchard.ContentManagement.Records.ContentItemVersionRecord civ
join civ.ContentItemRecord ci
join ci.ContentType ct
left outer join ci.TermsPartRecord tpr
left outer join tpr.Terms terms
where ct.Name = 'Page' AND (terms.TermRecord.Id in (:ids) OR size(tpr.Terms) = 0)

Related

Many to many AQL query

I have 2 collections and one edge collection. USERS, FILES and FILES_USERS.
Im trying to get all FILES documents, that has the field "what" set to "video", for a specific user, but also embed another document, also from the collection FILES, but where the "what" is set to "trailer" and belongs to the "video" into the results.
I have tried the below code but its not working correctly, im getting a lot of duplicate results...its a mess. Im definitely doing it wrong.
FOR f IN files
FILTER f.what=="video"
LET trailer = (
FOR f2 IN files
FILTER f2.parent_key==f._key
AND f2.what=="trailer"
RETURN f2
)
FOR x IN files_users
FILTER x._from=="users/18418062"
AND x.owner==true
RETURN DISTINCT {f,trailer}
There may be a better way to do this with graph query syntax, but try this. Adjust the UNIQUE functions based on your data-model.
LET user_files = UNIQUE(FOR u IN FILES_USERS
FILTER u._from == "users/18418062" AND u.owner
RETURN u._to)
FOR uf IN user_files
FOR f IN files
FILTER f._key == uf AND f.what == "video"
LET trailers = UNIQUE(FOR t IN files
FILTER t.parent_key == f._key AND t.what == "trailer"
RETURN t)
RETURN {"video": f, "trailers": trailers}
Well, check to see If you have duplicate data as suggested by TMan, however check your query syntax too. It appears that you have no link between your f subquery and the x in the main query. That would cause the query to potentially return a lot of dups if there are multiple records in collection files_users for user users/18418062
Try adding a join in the main query. Something like:
FOR x IN files_users
FILTER x._from=="users/18418062"
AND x.owner==true
AND x._to == f._id
RETURN DISTINCT {f,trailer}
On a related note, if you run into performance issues doing a subquery for trailers , you could instead try just doing a join and array expansion and see if that works for your case

ArangoDB: filter by collection type in a traversal query

I am using ArangoDB 2.8
I am doing a traversal query that includes 2 different collections. However in my result I would like to get only a particular collection, but I don't see the way to filter by collection name.
In my case I have address collection and user collection. In address collection I distinguish 3 levels as: {addressType: state}, {addressType: city} and {addressType: street}. Then I have an edge that links from address to user collection (state>city>street>user). I want to do a traversal (like in the code below) from an address (of any type) to the user (if any) and only return the collection of type user -for example if a street does not have a link to a user then return empty-.
For p in TRAVERSAL(address, myEdge, #vertex_id, 'outbound', {paths:false})
RETURN p.vertex._id)
Another answer is using the IS_SAME_COLLECTION function as hinted by this SO answer:
FOR p IN TRAVERSAL(address, myEdge, #vertex_id, 'outbound', {paths:false})
FILTER IS_SAME_COLLECTION('user', p.vertex._id)
RETURN p.vertex._id)
or, since TRAVERSAL was removed in ArangoDB 3.0+ (see this answer and the migration guide), something like
FOR v IN 0..5 IN OUTBOUND #vertex_id myEdge
FILTER IS_SAME_COLLECTION('user', v._id)
RETURN v._id)
Well I had to find a solution, so here is mine (I know is not the best but worked for me):
What I do is split the _id by "/" and check whether the first part (collection name) is in ['user']
For p in TRAVERSAL(address, myEdge, #vertex_id, 'outbound', {paths:false})
FILTER (SPLIT(p.vertex._id, "/", 1)[0]) IN ['user']
RETURN p.vertex._id)

loopback relational database hasManyThrough pivot table

I seem to be stuck on a classic ORM issue and don't know really how to handle it, so at this point any help is welcome.
Is there a way to get the pivot table on a hasManyThrough query? Better yet, apply some filter or sort to it. A typical example
Table products
id,title
Table categories
id,title
table products_categories
productsId, categoriesId, orderBy, main
So, in the above scenario, say you want to get all categories of product X that are (main = true) or you want to sort the the product categories by orderBy.
What happens now is a first SELECT on products to get the product data, a second SELECT on products_categories to get the categoriesId and a final SELECT on categories to get the actual categories. Ideally, filters and sort should be applied to the 2nd SELECT like
SELECT `id`,`productsId`,`categoriesId`,`orderBy`,`main` FROM `products_categories` WHERE `productsId` IN (180) WHERE main = 1 ORDER BY `orderBy` DESC
Another typical example would be wanting to order the product images based on the order the user wants them to
so you would have a products_images table
id,image,productsID,orderBy
and you would want to
SELECT from products_images WHERE productsId In (180) ORDER BY orderBy ASC
Is that even possible?
EDIT : Here is the relationship needed for an intermediate table to get what I need based on my schema.
Products.hasMany(Images,
{
as: "Images",
"foreignKey": "productsId",
"through": ProductsImagesItems,
scope: function (inst, filter) {
return {active: 1};
}
});
Thing is the scope function is giving me access to the final result and not to the intermediate table.
I am not sure to fully understand your problem(s), but for sure you need to move away from the table concept and express your problem in terms of Models and Relations.
The way I see it, you have two models Product(properties: title) and Category (properties: main).
Then, you can have relations between the two, potentially
Product belongsTo Category
Category hasMany Product
This means a product will belong to a single category, while a category may contain many products. There are other relations available
Then, using the generated REST API, you can filter GET requests to get items in function of their properties (like main in your case), or use custom GET requests (automatically generated when you add relations) to get for instance all products belonging to a specific category.
Does this helps ?
Based on what you have here I'd probably recommend using the scope option when defining the relationship. The LoopBack docs show a very similar example of the "product - category" scenario:
Product.hasMany(Category, {
as: 'categories',
scope: function(instance, filter) {
return { type: instance.type };
}
});
In the example above, instance is a category that is being matched, and each product would have a new categories property that would contain the matching Category entities for that Product. Note that this does not follow your exact data scheme, so you may need to play around with it. Also, I think your API query would have to specify that you want the categories related data loaded (those are not included by default):
/api/Products/13?filter{"include":["categories"]}
I suggest you define a custom / remote method in Product.js that does the work for you.
Product.getCategories(_productId){
// if you are taking product title as param instead of _productId,
// you will first need to find product ID
// then execute a find query on products_categories with
// 1. where filter to get only main categoris and productId = _productId
// 2. include filter to include product and category objects
// 3. orderBy filter to sort items based on orderBy column
// now you will get an array of products_categories.
// Each item / object in the array will have nested objects of Product and Category.
}

Telerik MVC Grid Filter

I was using Telerik MVC Grid in my project. I just wanted to change the dropdown value orders a bit. I googled for the requirement and found the filter dropdown options are handled by **telerik.grid.min.js file. But, I dont know how can i change the order from
Options by Default
Is Equal to
Is not equal to
Starts with
Contains
Does not contain
Ends with
Change to the below format
Contains
Does not contain
Starts with
Ends with
Is Equal to
Is not equal to
Can anybody tell me the possibilities that i can change the order of filter dropdown box ..
Thanks,
You can do it by JQuery simply by some codes like this :
$('#GRIDID').find(".t-filter").click(function () {
setTimeout(function () {
$(".t-filter-operator").html('<option value="substringof">Contains</option><option value="notsubstringof">Does not contain</option>');
});
});
NOTE : the above code is a sample, you should do a process to check what operators you have and then repopulate the items in desired order. You can find all allowed "option" tags by inspecting the rendered grid.
The easy way (if you're using MVC razor syntax)
#Html.Kendo().Grid<Model>().Columns(columns =>
{
columns.Bound(x => x.variableName);
})
.Filterable(filterable => filterable
.Extra(false)
.Operators(operators => operators
.ForString(str => str.Clear()
.Contains("Contains").DoesNotContain("DoesNotContain").WhateverYouNeed)
.ForEnums( dat => dat.Clear()
.IsEqualTo("Is Equal To"))
))
Here, the Clear() empties the options in the filter and then you can add the ones you want or even create your own custom ones there, simply place them there in the order that you want.
Have fun!

Complex queries in linq to nhibernate

We are using accountability pattern for organizational structure. I using linq to nhibernate to find some departments and position but I have two problem.
var query =
Repository<Party>.Find(p => p.IsInternal == true)
.Where(p => p.Parents.Any(c => c.Parent.PartyId == id))
.Where(p =>
(
p.PartyType.PartyTypeId == (int)PartyTypeDbId.Department &&
_secretariat.Departments.Select(c => c.PartyId).Contains(p.PartyId)
)
||
(
p.PartyType.PartyTypeId == (int)PartyTypeDbId.Position &&
p.Children.Any(c =>
c.AccountabilityType.AccountabilityTypeId == (int)AccountabilityTypeDbId.TenurePersonOfPosition &&
((Person)c.Child).UserName != null)
)
);
First : I got 'Unhandled Expression Type: 1003' for this part of query : '_secretariat.Departments.Select(c => c.PartyId).Contains(p.PartyId)'
and I got Property not found 'UserName'
We have many complex queries i think we need to use stored procedure.
Sorry for bad Inglish!
One nice thing that you can do with LINQ is break your queries into multiple parts. Since you are building an expression tree that won't get executed until the results are enumerated, you don't have to do it all in one line (like SQL).
You can even make some reusable "filters" that you can apply to IQueryable. These filter functions accept an IQueryable as an argument, and return one as a result. You can build these as extension methods if you like (I like to).
As for your immediate problem, you may want to try a join on _secretariat instead of attempting a subquery. I've seen them work in scenarios where subqueries don't.
In addition to Lance's comments you might want to look at a compiled Linq query and breaking up some of the responsibilties to follow SOLID principles.
I've just also found out that there are issues with the Contains when containing Any linq methods. However, Any seems to work well within Any, hence:
_secretariat.Departments.Select(c => c.PartyId).Any(x => x == p.PartyId)

Resources