Cocoa: How to avoid core-data duplicated relationships? - xcode

I have these entities:
ProductsEntity
name
orders [ProductsOrderRelationship]
OrderEntity
products [ProductsOrderRelationship]
ProductsOrderRelationship
order [OrderEntity]
product [ProductsEntity]
quantity
Now, I want to edit an existing order. I have a list of products available and cart.
Now I want to add these available products to the cart.
The code must check if the products exist, so it only increases the quantity.
But, by now, it is simply adding more relationships..
Let me share a piece of code!
The interface has a list on the left with the available products and a list on the right with the cart (order entity). Both have array controllers linked to my code. Then I have this action:
- (IBAction)addSelectedProducts:(id)sender {
NSArray *firstSelectedProducts = [availableProductsController selectedObjects];
//Objects selected in the array controller
NSMutableArray *selectedProducts = [[NSMutableArray alloc] initWithCapacity:1];
//Here I will filter the repeated ones
NSMutableSet *newProducts = [NSMutableSet set];
//This is the final value to change in the current order entry.
NSMutableSet *oldProducts = [orderManagedObject valueForKey:#"products"];
//This is the old value I will change.
//Here we filter every repeated entries:
if ( [firstSelectedProducts count] > 0 ) {
for (id object in firstSelectedProducts) {
if (![oldProducts containsObject:object]) {
[selectedProducts addObject:object];
}
}
}
//Here we create objects in the relationship entity:
for (int i = 0; i < [selectedProducts count]; i++) {
// Create new relationship.
NSManagedObject *newProductObject = [
NSEntityDescription
insertNewObjectForEntityForName:#"ProductsOrderRelationship"
inManagedObjectContext:managedObjectContext
];
[newProductObject setValue:[selectedProducts objectAtIndex:i] forKey:#"product"];
[newProductObject setValue:orderManagedObject forKey:#"order"];
[newProducts addObject:newProductObject];
[newProductObject release];
}
[newProducts unionSet:oldProducts];
//Join old products and new products.
[orderManagedObject setValue:newProducts forKey:#"products"];
//Re-set the value.
//(... release stuff here)
}
I can't find a guide to this specific problem.. Any suggestions?

I'm guessing that firstSelectedProducts contains ProductsEntity objects and oldProducts contains ProductsOrderRelationship objects. If that's true, the problem is that...
if (![oldProducts containsObject:object]) {
...will never match anything.
(What you call ProductsOrderRelationship is often called a LineItem. Changing the name of the class and its associated variables might make the logic clearer.)

Related

JavaFX8: Strings are not displayed in TableView

I have the following problem: I try to populate a tableview in JavaFX8 with an array. So, I try to add the array as a row to the tableview. I run this code in the Controller of my FXML file, when enter is pressed. This is the code:
String[] words = {"ace", "boom", "crew", "dog", "eon"};
List<String> tableViewRow = Arrays.asList(words);
ObservableList<String> row = FXCollections.observableList(tableViewRow); //observableArrayList also doesn't work
transactionOverview.getItems().add(row);
transactionOverview is my tableview, and the String[] is just a placeholder for my actual String[]. I tried to create my tableview in multiple ways:
public TableView<ObservableList<String>> transactionOverview;
public TableView<ObservableList> transactionOverview;
public TableView transactionOverview;
None of them works.
The problem is that tableview gets an extra row, which I can select, but there are now string values visible in the tableview. I don't know if they are added.
My code is based on Javafx 2.2 - Dynamic table view - table data (answer from Jitendra Pareek), and I have chosen for this solution because I don't want to use an extra class to populate my tableview.
Any help is appreciated!
Since (according to your comments) you have a fixed number of columns, I would strongly recommend creating a model class to hold the items in each row of the table. You can then follow the standard patterns and it should work readily.
However you manage a TableView, you must provide a cell value factory for each column. This is essentially a function that specifies how to get the value for a cell from the item in the row. If your use a model class that uses JavaFX properties, then you can use a PropertyValueFactory (though Java 8 lambda expressions make that pretty much redundant). Otherwise, you need to implement a callback.
If you really want to use a list structure to hold the data for each row, and assuming your table and table columns are all defined in the FXML file, you would do something like this in your controller class:
#FXML
private TableView<ObservableList<String>> transactionOverview ;
// ...
public void initialize() {
for (int i=0; i < transactionOverview.getColumns().size(); i++) {
TableColumn<ObservableList<String>, String> col = transactionOverview.getColumns().get(i);
final int colIndex = i ;
col.setCellValueFactory( (CellDataFeatures cellData) -> {
ObservableList<String> rowData = cellData.getValue();
return new ReadOnlyStringWrapper(rowData.get(colIndex));
});
}
// ...
}

Restkit: How to use ResponseMapping without id attributes for a relationship with RKAssignmentPolicyUnion for a one to many relationship

In RestKit, when using a RKEntityMapping, if a relationship mapping does not have any identification attributes set, by default, the entire set of child objects get replaced even if the relationship assignment policy is set to RKAssignmentPolicyUnion.
Is there a way to get around this limitation?
It seems that in case of a relationship mapping, all existing objects are loaded, if no identificationAttributes are present. Then it simply returns the first available object
from RKManagedObjectMappingOperationDataSource:
- (id)mappingOperation:(RKMappingOperation *)mappingOperation targetObjectForRepresentation:(NSDictionary *)representation withMapping:(RKObjectMapping *)mapping inRelationship:(RKRelationshipMapping *)relationship
// If we are mapping within a relationship, try to find an existing object without identifying attributes
// NOTE: We avoid doing the mutable(Array|Set|OrderedSet)ValueForKey if there are identification attributes for performance (see issue GH-1232)
if (relationship) {
NSArray *identificationAttributes = [entityMapping.identificationAttributes valueForKey:#"name"];
id existingObjectsOfRelationship = identificationAttributes ? [mappingOperation.destinationObject valueForKeyPath:relationship.destinationKeyPath] : RKMutableCollectionValueWithObjectForKeyPath(mappingOperation.destinationObject, relationship.destinationKeyPath);
if (existingObjectsOfRelationship && !RKObjectIsCollection(existingObjectsOfRelationship)) existingObjectsOfRelationship = #[ existingObjectsOfRelationship ];
for (NSManagedObject *existingObject in existingObjectsOfRelationship) {
if (! identificationAttributes && ![existingObject isDeleted]) {
managedObject = existingObject;
[existingObjectsOfRelationship removeObject:managedObject];
break;
}
NSDictionary *identificationAttributeValues = [existingObject dictionaryWithValuesForKeys:identificationAttributes];
if ([[NSSet setWithArray:[identificationAttributeValues allValues]] isEqualToSet:[NSSet setWithObject:[NSNull null]]]) {
managedObject = existingObject;
break;
}
}
}

Magento- configurable products options order to match the order they are in the attribute set

I have a magento site I'm building (1.6) my site has a bunch of configurable options with 6 or so attributes set as dropdowns for the customer to pick from. After saving a configurable product the order of the attributes changes. I've been able to find what I think is happening, it is reordering them according to the attribute id not the order I have them set up in the attribute set. I need to find a way to get magento to keep the order of the attributes the same as they are in the attribute set. Any help is greatly appreciated.
Trick is pretty simple.
Just drag'n'drop them in product->edit->associatedProduct tab ;)
The order of attributes from this page is saved to catalog_product_super_attribute table.
I was also looking for the same and finally i found this and it works for me hope it will work for others too.
From Admin Panel > Catalog > Attributes > Manage Attributes select the one like if you want to make it like for the capacity 4GB > 8GB > 16GB and so on then do this small changes.
Select Manage Label / Options > Manage Options (values of your attribute) and if you already created the variables just add the position manually, like:
4GB - 1
8GB - 2
16GB - 3
Save and flush the cache.
That's it, now it should show the attributes as per the position that you assign.
It is an old question but I have found a solution right now having the same problem.
If you are still interesting in changing the order of the configurable attribute you may want to look into this method:
Mage_Catalog_Model_Product_Type_Configurable::getConfigurableAttributes()
getConfigurableAttributes() load the collection of attributes.
The first time the collection is loaded, before saving the configurable, there is no position value, so I think the attribute ID rules on the display order.
If you want to alter this order you can only add a sort for attribute_id after the ->orderByPosition() and revert the order ( this will preserve the position functionality )
For example, here I have added ->setOrder('attribute_id','DESC')
public function getConfigurableAttributes($product = null)
{
Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
if (!$this->getProduct($product)->hasData($this->_configurableAttributes)) {
$configurableAttributes = $this->getConfigurableAttributeCollection($product)
->orderByPosition()
->setOrder('attribute_id','DESC')
->load();
$this->getProduct($product)->setData($this->_configurableAttributes, $configurableAttributes);
}
Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
return $this->getProduct($product)->getData($this->_configurableAttributes);
}
OR
In case you want to modify the order in more radical way, you can also act on this method:
Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config::getAttributesJson()
This is basically calling the getConfigurableAttributes().
To understand if this is the first configurable load, you can check all the attributes in the array $attributes to see if they all have a position ==0 and then proceed with a manual reorder )
Example
I'm omitting all the module creation and the rewrite part.
Here an example modifying getAttributesJson() in order to have the color attribute always on the top.
public function getAttributesJson()
{
$attributes = $this->_getProduct()->getTypeInstance(true)
->getConfigurableAttributesAsArray($this->_getProduct());
if (!$attributes) {
return '[]';
} else {
// == START ==
// checking if I can re-order
if ($this->isNoSavedPosition($attributes)) {
$attributes = $this->attributeReorder($attributes);
}
// == END ==
// Hide price if needed
foreach ($attributes as &$attribute) {
if (isset($attribute['values']) && is_array($attribute['values'])) {
foreach ($attribute['values'] as &$attributeValue) {
if (!$this->getCanReadPrice()) {
$attributeValue['pricing_value'] = '';
$attributeValue['is_percent'] = 0;
}
$attributeValue['can_edit_price'] = $this->getCanEditPrice();
$attributeValue['can_read_price'] = $this->getCanReadPrice();
}
}
}
}
return Mage::helper('core')->jsonEncode($attributes);
}
public function isNoSavedPosition($attributes)
{
foreach ($attributes as $attribute) {
if (isset($attribute['position']) && $attribute['position'] != 0) {
return false;
}
}
// there is no position saved
// - this is the first time the configurable is loaded
// - (the position is saved on second save action)
return true;
}
public function attributeReorder($attributes)
{
// we want the Color attribute to be always on the top
$newAttributesOrderArray = array();
foreach ($attributes as $key => $attribute) {
if (isset($attribute['label']) && $attribute['label'] == 'Color') {
$newAttributesOrderArray[] = $attribute;
unset($attributes[$key]);
}
}
$newAttributesOrderArray = array_merge($newAttributesOrderArray,$attributes);
return $newAttributesOrderArray;
}

Core Data Migration: Attribute Mapping Value Expression

I currently have a cardType attribute on my entity, which in the old model could be "Math", "Image" or "Text". In the new model, I'll be using just "Math" and "Text" and also have a hasImage attribute, which I want to set to true if the old cardType was Image (which I want to change to "Text").
Lastly, I have a set of another entity, "card", of which a set can be associated with a deck, and in each of those, I'll also have hasImage which I want to set to true if the deck was of "Image" type before.
Is this all possible using the Value Expression in the Mapping Model I've created between the two versions, or will I have to do something else?
I can't find any document telling me exactly what is possible in the Value Expression (Apple's doc - http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmMappingOverview.html%23//apple_ref/doc/uid/TP40004735-SW3 - only has a very simple transformation). If I have to do something else, what would that be? This seems simple enough that an expression should be able to do it.
One thing you can do is create a custom migration policy class that has a function mapping your attribute from the original value to a new value. For example I had a case where I needed to map an entity called MyItems that had a direct relationship to a set of values entities called "Items" to instead store an itemID so I could split the model across multiple stores.
The old model looked like this:
The new model looks like this:
To do this, I wrote a mapping class with a function called itemIDForItemName and it was defined as such:
#interface Migration_Policy_v1tov2 : NSEntityMigrationPolicy {
NSMutableDictionary *namesToIDs;
}
- (NSNumber *) itemIDForItemName:(NSString *)name;
#end
#import "Migration_Policy_v1tov2.h"
#implementation Migration_Policy_v1tov2
- (BOOL)beginEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error {
namesToIDs = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1],#"Apples",
[NSNumber numberWithInt:2],#"Bananas",
[NSNumber numberWithInt:3],#"Peaches",
[NSNumber numberWithInt:4],#"Pears",
[NSNumber numberWithInt:5],#"Beef",
[NSNumber numberWithInt:6],#"Chicken",
[NSNumber numberWithInt:7],#"Fish",
[NSNumber numberWithInt:8],#"Asparagus",
[NSNumber numberWithInt:9],#"Potato",
[NSNumber numberWithInt:10],#"Carrot",nil];
return YES;
}
- (NSNumber *) itemIDForItemName:(NSString *)name {
NSNumber *iD = [namesToIDs objectForKey:name];
NSAssert(iD != nil,#"Error finding ID for item name:%#",name);
return iD;
}
#end
Then for the related Mapping Name for the attribute in your mapping model you specify the Value Expression as the result of your function call as such:
FUNCTION($entityPolicy,"itemIDForItemName:",$source.name) **
You also have to set the Custom Policy Field of your Mapping Name for that attribute to your mapping class name (in this case Migration_Policy_v1tov2).
**note this should match the selector signature of the method

Load three UITableViews from different datasources

I have three uiTableViews on a view.
I have created three different NSMutableArray loaded with different data.
I need to place one of the NSMutableArray as the datasource for one of the UITableView.
I am able to assign all three UITableViews datasource through the viewDidLoad of the form.
But what I really need to do, is assign each of the UITableView datasource to a different NSMutableArray.
How can I perform this task?
thanks
tony
If all three UITableViews share the same datasource object (the object that holds all three of your arrays), then just use if statements to differentiate between the table views asking for the data:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
// If table 1 is asking, give it table 1 data...
if (tableView == myTableView1)
{
// Assume all sections have 3 rows each for
// purposes of simple demonstration...
return [dataSourceForTable1 count];
}
// If table 2 is asking, give it table 2 data...
if (tableView == myTableView2)
{
// Assume all sections have 3 rows each for
// purposes of simple demonstration...
return [dataSourceForTable2 count];
}
// If table 3 is asking, give it table 3 data...
if (tableView == myTableView3)
{
// Assume all sections have 3 rows each for
// purposes of simple demonstration...
return [dataSourceForTable3 count];
}
// The compiler will complain if we don't have
// a final return since it's possible none of the
// if statements will be true ...
return 0;
}

Resources