Page 1 of 1
Unidirectional many-to-many
Posted: Wed Feb 06, 2013 9:20 am
by matthew.walters
Hi,
Is it possible to display a unidirectional many-to-many?
The entity in the main presentable is the owner of the many-to-many relationship with the entity of a subpresentable.
It's unidirectional, the main entity has a list of the subpresentable's entity, but not vice-versa
My subpresentable is of type SimpleSearchJoinStructurePresenter.
I see that in Broadleaf, this is done for Category / Media. It's a many-to-many relationship and category has media, but not vice-versa, but this is a mapstructure, whereas, I have a list.
BTW I'm using v1.61
Thanks,
Matthew
Re: Unidirectional many-to-many
Posted: Wed Feb 06, 2013 9:58 am
by phillipuniverse
With maps you can do unidirectional, but not with lists. They need to be bidrectional to map them properly in the admin.
Re: Unidirectional many-to-many
Posted: Thu Feb 07, 2013 12:19 pm
by matthew.walters
Thanks for the quick reply.
So I've implemented as bidirectional and it's working.
One more thing I'm stumped on:
Now I'm attempting to use a custom persistence handler to fetch the list of entities which populate the search grid (I want to filter some out).
The issue I'm having is that my custom persistence handler's fetch method is also being used to fetch the entities associated with the main entity (rather than fetching the entities which are actually associated with it, via foreign key)
To get around this, in the changeSelection method of the presenter, i've set the custom criteria of the datasource used by the subpresentable so that the criteria checked in the 'canhandlefetch' method of the custom persistence handler is null, so that it won't use the custom fetch when loading the record. that's working fine, the correct associated entities are now being loaded when loading a record.
My question is how can I re-enable this custom criteria only when clicking on the "add" button, so that the custom fetch is used to populate the entities in the list?
Thanks,
Matthew
Re: Unidirectional many-to-many
Posted: Thu Feb 07, 2013 1:13 pm
by phillipuniverse
In the canHandleFetch method, you can check the persistencePackage.getCeilingEntityFullyQualifiedClassname() to ensure that it is only active for the particular ceiling entity you are trying to fetch. I feel like that is the correct solution to your problem, although I feel like I'm missing something with your description. I'm not sure what you mean by this part:
The issue I'm having is that my custom persistence handler's fetch method is also being used to fetch the entities associated with the main entity (rather than fetching the entities which are actually associated with it, via foreign key)
To specifically answer your question though, your presenter you could override the 'addClicked' method and add something like this:
Code: Select all
public void addClicked(final String newItemTitle) {
super.addClicked(newItemTitle);
((CustomCriteriaListGridDataSource)subPresenter.getDisplay.getDataSource()).setCustomCritera(new String[]{"someCriteria"});
You might also be able to glean some helpful information from here:
viewtopic.php?f=13&t=528&p=1595&hilit=custom+criteria#p1595
Re: Unidirectional many-to-many
Posted: Thu Feb 07, 2013 2:44 pm
by matthew.walters
Let me explain the situation a little more fully
Entities A and B have bidirectional many-to-many relationship.
Entity A has a Presenter and a view, and its presenter contains a SubPresentable of type SimpleSearchJoinStructurePresenter for B
it's analogous to going to Category, selecting a particular category, then for that selected category, going to the "Products" tab and seeing the list of "all products"
except, now, in this analogy, if I click on "add" in the "all products" tab, I want to do a custom fetch to filter the entities that appear in the "search for a product" popup.
when you suggested:
Code: Select all
public void addClicked(final String newItemTitle) {
super.addClicked(newItemTitle);
((CustomCriteriaListGridDataSource)subPresenter.getDisplay.getDataSource()).setCustomCritera(new String[]{"someCriteria"});
I believe you were referring to the adding a new catalog (in this analogy), ie a new main presentable entity, but I want to add a new product to the list of "all products" (in this analogy), ie a new sub presentable entity
Re: Unidirectional many-to-many
Posted: Thu Feb 07, 2013 3:21 pm
by matthew.walters
Okay I've solved this, mostly
In the changeSelection method of the main presentable,
I modify the custom criteria of the subpresentable's data source, replacing the string that the canfetch method looks for, with null.
Then I call the load method on the subpresentable.
Then I modify the custom criteria of the subpresentable, replacing the null value (from above) with the string that the canfetch method looks for.
So now everything is working except, when I click "add" for the subpresentable, i cannot filter in the listgrid that appears in the popup. so I try to filter for "AAAAA", but since the subpresentable is using a custom fetch, rather than repopulating the listgrid with only entities having a name containing "AAAA", it just repopulates with the same list (the list retrieved from the custom fetch). So I need another custom fetch when filtering on the list that appears in the popup to retrieve only entries from the original custom fetch, whose name matches what was entered in the filter.
Is there a way to modify custom criteria of a data source when clicking on the filter button in the listgrid popup when adding a new subpresentable ? sorry that was very wordy.
for an analogy:
in the category section, select a category, then go its "products" tab, you see the list of all products associated to that category. click on "add", the "search for a product" popup opens. now in my case, i'm using a custom fetch to retrieve the list of entities that can be selected. however if I specify a value in the filter, such as product name, and click the filter button, it just reuses my custom criteria (which will just display the same list), rather than filtering on the results of the custom criteria based on product name.
Re: Unidirectional many-to-many
Posted: Thu Feb 07, 2013 4:53 pm
by phillipuniverse
Ah gotcha; I understand. That criteria is available in the 'fetch' method via the CriteraTransferObject parameter. You can utilize it like so:
Code: Select all
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Map<String, FieldMetadata> properties = helper.getSimpleMergedProperties(persistencePackage.getCeilingEntityFullyQualifiedClassname(), persistencePerspective);
BaseCtoConverter ctoConverter = helper.getCtoConverter(persistencePerspective, cto, persistencePackage.getCeilingEntityFullyQualifiedClassname(), properties);
PersistentEntityCriteria queryCriteria = ctoConverter.convert(cto, persistencePackage.getCeilingEntityFullyQualifiedClassname());
This converted PersistentEntityCriteria is then intended to be passed to the dynamicEntityDao:
Code: Select all
List<Serializable> records = dynamicEntityDao.query(queryCriteria, Class.forName(persistencePackage.getCeilingEntityFullyQualifiedClassname()));
Entity[] entities = helper.getRecords(properties, records, null, null);
As you said, in your case you have to add additional filtering. What you might try is instead of pulling back the list of entities that you currently are, try utilizing the provided CriteriaTransferObject and tack additional criteria onto that (check the add() method). This will give you the best of both worlds.