Custom compound rows in NSPredicateEditor - core-data

Say we have two Core Data entities:
entity Bar
attribute bar1;
attribute bar2;
entity Foo
attribute foo1;
attribute foo2;
to-many relationship <Bar*> bars
I want to use NSPredicateEditor to search for "any Foos that have bars with (bar1='term1' and bar2='term2')". That is, multiple matches in the same Bar.
With some kind of custom compound predicate that has a UI like this
Image 1
Image 2
I know how to create top-level NSPredicateEditorRowTemplates to search for Foos based on Bar attributes (Foo.bars.bar1).
But those are compounded along with the Foo attribute subpredicates, so I can't limit the search to multiple attributes in the same Bar.
When selecting Bar attributes to search, the list should limit itself to the Bar attributes.
Is it even possible to have multiple types of compound predicates in the same NSPredicateEditor?
Surprisingly little code for customizing NSPredicateEditor out there...

Related

Dividing an inline form into columns based on model field value in django admin

I'm setting up an an admin page that handles the storage of data of a product, in this case it is shoes which most of you might realize usually comes in pairs of left and right. The shoes have a differing number of fields and that's why i've implemented an eav system where the forms for the shoes are contained in an inline.
All i would like to do now is to group the forms in the inline in two columns depending on the value of the field shoe_side (can be 'L' or 'R' for left and right)
This is regarding the change view since the system is set up so that it creates all the neccessary fields depending on another model we can call a product recipe.
I did this in javascript with templates before in a similar situation but that was without the inline fields so it's not applicable. I think if I could just group the fields of the two columns into two formsets I could edit the template to get the needed layout but I don't know if one can create formsets based on a field in the model, I haven't really grasped the concept of formsets.
# The inline model
class Component(models.Model):
part_name=models.ForeignKey(PartRecipe)
shoe_side=models.CharField(choices=[('L', "Left"),('R', "Right")
product_id = ForeignKey(ProductModel)
....
# The 'parent' model
class ProductModel(models.Model):
product = ForeignKey(Productrecipe)
....
# a lot of not relevant fields
# The admin classes
class ComponentInline(admin.StackedInline):
template = 'admin/componentinline/stacked.html'
model = Component
class ProductAdmin(admin.ModelAdmin):
...
inlines = [ComponentInline,]

Is it possible in Pony ORM to add extra attributes to the intermediate table of a many-to-many relationship?

In Pony ORM, it is possible to ‘automatically’ create a many-to-many relationship. For example, from the documentation (for version 0.6, emphasis mine):
In order to create many-to-many relationship you need to define both
ends of the relationship as Set attributes:
class Product(db.Entity):
tags = Set("Tag")
class Tag(db.Entity):
products = Set(Product)
In order to implement this relationship in the database, Pony will
create an intermediate table. This is a well known solution which
allows you to have many-to-many relationships in relational databases.
Is it possible to create an extra attribute (column) in the automatically created intermediate table, so not just foreign keys to ‘Product’ and ‘Tag’, but, e.g., also a timestamp?
If yes, how?
If not, I guess I'll have to create the intermediate table explicitly. Can I in that case still use the nice Set-attribute definition (perhaps to the intermediate table, indicating the attribute of interest)?
Currently it is necessary to define explicit entity, like this:
class Product(db.Entity):
name = Required(str)
tags = Set("ProductTag")
class Tag(db.Entity):
name = Required(str, unique=True)
products = Set("ProductTag")
class ProductTag(db.Entity):
product = Required(Product)
tag = Required(Tag)
PrimaryKey(product, tag)
timestamp = Required(datetime, default=datetime.now)
Pony does not support virtual Set attributes like through in Django, but we plan to add them in the future. Right now you need to work with intermediate table explicitly.
Adding a tag to a product
p1 = Product[1]
tag1 = Tag.get(name='smartphones')
p1.tags.create(tag=tag1)
# or:
ProductTag(product=p1, tag=tag1)
Removing a tag from a product:
ProductTag[p1, tag1].delete()
Checking if a product has a specific tag:
ProductTag.get(product=p1, tag=tag1) is not None
Also, Pony support the concept of attribute lifting. That means that in Pony any collection attribute has all attributes of its items. The value of a such collection attribute is a collection of all values for individual items. For example, in order to get all tags for specific product, you can write:
p1.tags.tag
The p1.tags expression returns a collection of ProductTag items. Each ProductTag object has tag property which points to a specific tag object. So p1.tags.tag returns a collection of all Tag objects linked with a specific Product object.
It is possible to use attribute lifting inside queries. For example, in order to found all products with the tag smartphones you can write the following query:
select(p for p in Product if 'smartphones' in p.tags.tag.name)
Here, p.tags is a colection of ProductTag objects, p.tags.tag is a collection of Tag objects, and p.tags.tag.name is a collection of tag names. The query above is a syntactic sugar for the following query:
select(p for p in Product if 'smartphones' in select(item.tag.name for item in p.tags))
Also, the query can be rewritten as:
select(pt.product for pt in ProductTag if pt.tag.name == 'smartphones')

JSF displaying entities with IDs: how to translate IDs to descriptions?

In a JSF page I have to display the data from an entity.
This entity has some int fields which cannot be displayed directly but need to be translated into a descriptive string.
Between them some can have a limited number of values, others have lots of possible values (such as a wordlwide Country_ID) and deserve a table on the Db with the association (ID, description).
This latter case can easily be solved navigating via relationship from the original entity to the entity corresponding to the dictionary table (ID, description) but I don't want to introduce new entities just to solve translations form ID to description.
Besides another integer field has special needs: the hundred thousand number should be changed with a letter according to a rule such as 100015 -> A00015, 301023 -> C01023.
Initially I put the translation code inside the entity itself but I know the great limits and drawbacks of this solution.
Then I created a singletone (EntityTranslator) with all the methods to translate the different fields. For cases where the field values are a lot I put them inside a table which is loaded from the singletone and transformed in a TreeMap, otherwise the descriptions are in arrays inside the class.
In the ManagedBean I wrote a getter for EntityTranslator and inside the jsf I use quite long el statements like the following:
#{myManagedBean.entityTranslator.translateCountryID(myManagedBean.selectedEntity.countryID)}
I think the problem is quite general and I'm looking for a standard way to solve it but, as already stated, I don't want to create new 'stupid' entities only to associate an ID to a description, I think it is overkill.
Another possibility is the use of converters Object(Integer) <-> String but I'm more comfortable in having all the translation needs for an Entity inside the same class.
Your question boils down to the following simple line:
How can I display a field different from id of my entity in my view and how can I morph an integer field into something more meaningful.
The answer is that it depends on a situation.
If you solely want to input/output data, you don't need id at all apart from the possible view parameter like ?id=12345. In this case you can input/output anything you want in your view: the id is always there.
If you want to create a new entity most possibly you have a way of generating ids via JPA, or database, or elsehow besides the direct input from the user. In this situation you don't need to mess with ids as well.
If you want to use information on other entities like show user a dropdown box with e.g. a list of countries, you always have the option to separate label (let it be name) and value (let it be id), or even have a unique not null column containing the country name in your database table that will serve as a natural identifier. If you'd like to get data from the user using an input text field you always can create a converter that will do the job of transforming user input strings to actual entity objects.
Regarding the transformation of your integers, you've actually got several choices: the first one is to attach a converter for these fields that will roughly do 301023 -> C01023 and C01023 -> 301023 transformations, the second one is to write a custom EL function and the third one is to prepare the right model beforehand / do the transformations on-the-fly.

how to render custom layout of the projection of known content items

I have defined my own projection by a query which returns a set of content items of known content type. I would like to take pick up certain content parts of these content items and display them in the list. Using shape tracing tool I have found the view template where to write my custom layout:
/Views/Parts.ProjectionPart.cshtml
but from the Model variable in the template I can not get the data I want because it is way too high above from the content parts data.
a good example of what I want: let's say I want to render product catalog as defined in this tutorial:
http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-5
but I want only to render a list which consists from items:
name of the owner who created the product
name of the product.
publish date of the product
and I need to render it at one place, i.e., not separately in their own part views.
Have you tried adding a layout in the projector module? There is a properties mode option that lets you select which fields/data to show. If the data you want is not there, you should be able to implement an IPropertyProvider. There are examples of this in the Projections module code.

DDD: aggregate root question

Let's say i got 2 entities - Foo and Bar. Foo is an aggregate root and contains Bar. As far as i understand, it should look like this:
public class Foo{
private readonly Bar Bar;
}
I want to provide functionality for users to choose Bars for Foos from a defined list (and change it).
If repositories are supposed to be for aggregate roots only it means that there will be no repository for Bar entity.
This leads to problem - Bar can't be created/updated independently without a reference to Foo.
Does that mean that Bar is supposed to have a repository despite that it has no meaning without a Foo?
If you want to select from a list of Bars where they're not associated with Foo, then this is not an aggregate root. For example, you can't get list of OrderItems without their Order, so this is single aggregate root (Order), but you can get list of Products to assign to OrderItems, so Product is not part of the Order aggregate root.
Notice that while OrderItem is part of Order aggregate root, you can still create and update it independently. But, you cannot get it without reference to Order. Same for your Bar, even if it was part of Foo, you could get each(Foo.Bars) and work with it, or do Foo.AddBar(new Bar()). But if you need to get List without Foo, Bar is not part of Foo aggregate. It is a separate entity.
Well, that's how I see DDD here, but I'm not Eric Evans, of course.
The reasons for having Aggregate roots are:
They provide controlled and directed access to composite entities
They can enforce rules to ensure that the entire aggregate is valid
My take:
If you need to select Bar objects without a Foo, use a BarRepository.
But...
What if you update a Bar, and it breaks a validation rule for it's parent Foo? If this could happen, you should access Bar via it's parent Foo.
If, however, you need to access a bunch of Bar objects (e.g for a batch job or report), and you know that Foos won't get broken, go ahead and access them via BarRepository.
Remember that aggregate roots can be composed of other aggregate roots. You may discover that Bar is an aggregate root itself, giving you justification for a BarRepository :)
Are you sure that Bar need to be a entity? Do you have the need to track it and change it in the domain? If you can look at it as a value object, I would suggest that you fetch it from a service and then "connect" the selected value object to the Foo entity. For instants through a dropdown list.

Resources