Hello. Ok the background. I made a fetch from of a "Product" on the database like this: try {
//session().defaultEditingContext().saveChanges(); Product p = (Product)EOUtilities.objectMatchingKeyAndValue(session().defaultEditingContext(), "Product", Product.CODE_KEY, code); //If I get thi this line its because the producto already exists so I will inform the user return this.informUser(p); } catch (EOObjectNotAvailableException e) { // TODO Auto-generated catch block //If I came into the catch is because the product does not exist so it's save to saveCahnges(); now, as you can see if the product exists, I will inform the user, but I decided to upgrade the number of itmes at the same time, so I did the following, based on an Apple support page:
/** * Method that will update the old quantity with the new one, in case the Procut already exists * @param p * @return */ private WOActionResults informUser(Product p) {
EOAdaptor adaptor; EOAdaptorContext adContext; EOAdaptorChannel adChannel; EOFetchSpecification fspec =new EOFetchSpecification(Product.ENTITY_NAME, null, null); EOObjectStoreCoordinator rootStore = (EOObjectStoreCoordinator) session().defaultEditingContext().rootObjectStore(); EODatabaseContext dbContext = (EODatabaseContext) rootStore.objectStoreForFetchSpecification(fspec); EODatabaseChannel dbChannel = dbContext.availableChannel(); adaptor = dbContext.database().adaptor(); adContext = dbContext.adaptorContext(); adChannel = dbChannel.adaptorChannel();
NSDictionary dic = new NSDictionary((new Integer(Integer.parseInt(itemQty)))+p.numberItemsInStock(),Product.NUMBER_ITEMS_IN_STOCK_KEY); EOQualifier qual = Product.CODE.eq(code); EOEntity myEntity = (EOModelGroup.defaultGroup().modelNamed("StockManager")).entityNamed(Product.ENTITY_NAME);
System.out.println("Opening Channel"); adChannel.openChannel(); adChannel.updateValuesInRowDescribedByQualifier(dic, qual, myEntity); System.out.println("Closing Channel"); adChannel.closeChannel();
// TODO Auto-generated method stub errorMessage = "The product with Code Number: "+ code + ", already exists in the DataBase, the Quantity has been updated it "; AjaxHighlight.highlight("errMess"); return pageWithName("RegisterProducts"); } Then I tested and wow, it worked then I read it, and WOW, got confused, I had to back to the EO PRogramming guide and read again what are those EOAdaptor things, and it was quite confusing. Then what I did, was that once the first time I updated the quantity, I just immediately save the the registration again, to see what would happen, the app told me that eventually the number of Items in stock were updated, I checked the db again, but for my surprise this time was reduced.
Ok questions.: is that the right method to make an update in a a db?. is there another way to get the EOModel, so I can get the Entity, and then use the method from EOAdapterChannel updateValuesinRowDescribedByQualifiers?, and, why it might happened that after the first update, if I click once again the submit button, the numbers of items were reduced?
Well if any better explanation of what I did here that will help me clear my mental map, I will appreciate it also.
Thanks
Gus |