Ramsey,
so after all of 15 minutes (and being disturbed by a 10 minute phone call) and it works!!!!! Thank you! Now on to a JasperReport for the week! two rules:
100 : pageConfiguration = 'CreateEvent' => displayPropertyKeys = (("one", "show", "dateTime"), ("tabTwo", "eventBooks")) [com.webobjects.directtoweb.Assignment] 100 : (pageConfiguration = 'CreateEvent' and tabKey = 'one') => validationKeys = ("populateEventBooks") [com.webobjects.directtoweb.Assignment]
and the method:
public void populateEventBooks() { EOQualifier bookForPrimaryPersonQualifier = null; NSArray<Book> booksForThisEvent = Book.fetchBooks(editingContext(), Book.SHOW.eq((Show) this.valueForKey(SHOW_KEY)), null);
for (Book aBook: booksForThisEvent) { bookForPrimaryPersonQualifier = ERXQ.and(Person.CURRENT.eq(true), Person.PERSON_BOOKS.dot(PersonBook.IS_PRIMARY_PLAYER.eq(true))); bookForPrimaryPersonQualifier = ERXQ.and(bookForPrimaryPersonQualifier, Person.PERSON_BOOKS.dot(PersonBook.BOOK.eq(aBook)));
Person aPerson = null; //try to get the person for this book and assign him to try { aPerson = Person.fetchPerson(editingContext(), bookForPrimaryPersonQualifier); } catch (Exception e) { e.printStackTrace(); }
//if there is a primary person applied to this book, assign him/her to the EventBook else it's null! if (aPerson != null) { EventBook eo = EventBook.createEventBook(editingContext(), aBook, this); eo.setPersonRelationship(aPerson); } else { EventBook.createEventBook(editingContext(), aBook, this); } } }
On Feb 12, 2015, at 6:29 PM, Ramsey Gurley < email@hidden> wrote: On Feb 12, 2015, at 1:38 PM, Theodore Petrosky < email@hidden> wrote: On Feb 12, 2015, at 12:15 PM, Ramsey Gurley <email@hidden> wrote:
On Feb 11, 2015, at 10:46 PM, Theodore Petrosky <email@hidden> wrote:
But there are rules about these books. There are 15 persons assigned to the Reed1 book for Show01. Only one of them is the PrimaryPlayer. Likewise there are 20 other books assigned to this Show.
How do you know who is the primary if you have 15 people assigned to the book? Enforce that in the model.
it is enforced in the model! No Person is assigned to a Book, I have a PersonBook Entity and a Person is assigned to a PersonBook (Book, Person, isPrimaryPlayer)
so in the above, I have 15 people assigned to a PersonBook where the Book is Reed1 and the PersonBook.book.show is Show01. Only one of these PersonBooks is marked "isPrimaryPlayer = true”
Without a unique index, I wouldn’t consider that enforced. Anyone can go in with a DBA tool and wreck your data. update person_book set is_primary_player = true 1326 rows updated!
Also, doesn’t this primary player credential have the possibility to change over time? What happens when a primary player is fired? Someone else steps in and now they are the primary player? How do you look back at past data and see who was the primary player for a book a year ago once the data starts mutating like that? Person toMany personBooks PersonBook toOne Person and toOne Book with a boolean isPrimaryPlayer
Event ->>eventBooks ->primaryEventBook
Now an event can only have one primary. Note, this creates a circular relationship. Your database has to be able to do deferred constraints. Pick a good one.
Notice that now you have created this relationship, your problem is solved. You can have a wizard interface
([tab1], show, date, [tab2], eventBooks, [tab3], primaryEventBook)
You select a show and date. When you go to the next page, you can auto populate your event books relationship with persons based on PersonShow. When you go to the next page, you can have the user select one of your existing EventBooks filtered by isPrimaryPlayer from PersonBook. If there’s only one, then it is automatically assigned. If there are none, then you auto populate a new event book with a null person.
I don’t understand how to accomplish what you said. I go to tab2 (eventBooks). How do I auto populate the eventBooks with primaryPlayers.
I’d use my custom controller logic in R2 :-) I think even ERD2W gives you a hook though. Check out ‘validationKeys' in ERD2WinspectPage.performAdditonalValidations() source. You can create a rule which will fire one or more methods on your eo when you click the next button on a wizard page. Don’t confuse this with actual NSValidation stuff. You can throw a validation exception from the method to prevent progress off the tab/step, but this is not happening in a saveChanges(). In these ‘validation’ methods, you’re perfectly okay just updating relationships and doing whatever you want. So you could do something like: 100: pageConfiguration = ‘CreateEvent' and tabKey = ’Tab1' => validationKeys = “(populateEventBooks)” Event.class === public void populateEventBooks() { NSArray<Person> persons = … for(Person person: persons) { //create event books } } Keep in mind that the User that is creating the Events, doesn’t know nor care about who is the auto populated people. Each Primary player has to log in and manage who is playing ‘their’ book at a given Event. So you are correct, if when auto populating the books to an event, there is no one assigned as the primary player, so what. the book is assigned and the player is null.
Ideally, in your example above, on Tab1 I select the show and the date. Go to tab2 and all the appropriate EventBooks for this show are applied.
I don’t see the need for tab2!
If you can populate eventBooks without user interaction, then just have the first and third tab. which brings me full circle to ask where (at what point in the process) to add these related EOs. It seems from the documentation that willInsert() happens very late. according to the docs:
/** * Called as part of the augmented transaction process. * This method is called after saveChanges is called on * the editing context, but before the object is actually * inserted into the database. This method is also called * before <code>validateForInsert</code> is called on this * object. This method is called by the editing context * delegate {@link ERXDefaultEditingContextDelegate}. */ public abstract void willInsert();
so willInsert means willInsertIntoDatabase
wow, AFTER saveChanges but before the object is actually inserted into the database. so what happens if there is an error (I don’t know what). does an empty event get saved, or does the app crash.
It crashes and no changes are saved. Same for willUpdate and willDelete.
|