How to import hierarchical folder style data - objective-c

I have an array of objects that I need to iterate over and find/insert into a core data entity. I just can't get my head around how to dive into albumParentFolders and see if they exist, if not add them to the entity albums.
I know it is some kind of recursive method that drills down into the albumParentFolders but due to how my AlbumObjects are structured cannot find a working solution.
So, my array of objects to import are like this
NSArray AlbumObjects
---------------------------------------
NSString * albumFolderName
NSString * albumDetailsTitle
NSString * albumDetailsURL
NSArray * albumParentFolders (Array of (NSStrings *) of albumFolderNames used for the level in the tree this album lives in. Can be nil for a root folder)
I need to first search core data and if not found insert the albumFolderName into a Core Data Entity called albums. Then using the auto generated class for the album entity, insert a new details object for that folder. e.g. [newFolder addDetailsObject:newDetails] Also, albumFolderName is not unique in core data as the albumFolderName could be listed multiple times but in different parent folders.
Entity: albums
-------------------------------------
Attribute: folderName
-------------------------------------
Relationship: albumDetails <------>> Entity : details (For adding multiple albumDetailsTitle and albumDetailsURL)
Relationship: parent <<-------
Relationship: subGroups |
^ |
------------
The album folders are eventually displayed in a treeview using NSOutlineView and IB bindings. This part already works.
If anybody could advise on how to iterate the AlbumObjects, specifically drilling into albumParentFolders so that the entire folder structure can be stored in the entity I would be hugely grateful.
SDK needs to support OSX 10.7+

Start at the root, follow the tree and create missing branches.
Pseudo code:
album = root
if (albumParentFolders != nil) {
parentAlbum = root
for (folder in albumParentFolders) {
album = find folder in parentAlbum.subGroups
if (album not found) {
album = insert new album
album.folderName = folder
album.parent = parentAlbum
}
parentAlbum = album
}
}
add details to album

Related

find nodes with a specific child association

I am looking for a query (lucene, fts-alfresco or ...) to return all the document which have a specific child association (that is not null).
Some context:
Documents of type abc:document have a child-association abc:linkedDocument.
Not all document have an other document linked to them, some have none some have one or multiple.
I need a fast and easy way to get an overview of all the documents that do have at least one document linked to them.
Currently I have a webscript that does what I need, but prefer not to have tons of webscripts which are not business related.
code:
SearchParameters sp = new SearchParameters();
String query = "TYPE:\"abc:document\"";
StoreRef store = StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
sp.addStore(store);
sp.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO);
sp.setQuery(query);
ResultSet rs = services.getSearchService().query(sp);
List<NodeRef> nodeRefs = rs.getNodeRefs();
for (NodeRef ref : nodeRefs) {
List<ChildAssociationRef> refs = services.getNodeService().getChildAssocs(ref);
for(ChildAssociationRef chref : refs){
if(chref.getQName().equals(AbcModel.ASSOC_LINKED_DOC)){
logger.debug("Document with linked doc: {}", ref);
break;
}
}
}
Associations aren't query-able so you'll have to do what you are doing, which is essentially checking every node in a result set for the presence of a desired association.
The only improvement I can suggest is that you can ask for the child associations of a specific type which would prevent you from having to check the type of every child association, see How to get all Child associations with a specific Association Type Alfresco (Java)

LOCATION type in QB CUSTOM OBJECT

With the last release of Quickblox SDK there is a new LOCATION field type, meaning we can save gps coordinates.
My question is: are or will be there specific functions/methods to query on this kind of field?
For instance, can I get all records in a CUSTOM OBJECT class that are located with 10 kms of my current location? - (based on a location field in the class)
Can I order the result by distance from my location?
These kind of queries are available in the LOCATION module.
Until now we were queering LOCATION to get something like "all users within 10 kms of my location" and then using those users ID's to query CUSTOM OBJECT and get specific information about those users.
With the new LOCATION field can we do this using only CUSTOM OBJECTS?
Try something like this:
NSMutableDictionary *params = [NSMutableDictionary new];
params[#"field_name[near]"] = #"34.0013123, 15.014915; 40000";
To avoid name conflicts, try to give a plural name to location field.

zend framework 2 + doctrine: removing media with related Entity

I've setup ZF2 Skeleton Application with Doctrine2. My goal is to create simple News service with simple hierarchy.
Category -> News -> Media (file)
I've setup all required relations for Category, News and Media (i.e. If News is deleted all related media is deleted from DB).
The problem is that media points to some file (located in file storage). I've implemented simple function that deletes all media related to News and then News it self.
$news->deleteImg();
$this->getEntityManager()->remove($news);
$this->getEntityManager()->flush();
It feels that this is wrong approach.
Is there a way to bind delete file function to Media Entity that will be called automatically each time Media is removed directly or throught it's parents? (i.e. News or Category)
Found the solution.
It's pretty simple:
First add annotation before Media class
/**
* Media
*
* #ORM\Table(name="media")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks <- Add this line
*/
class Media
Then you need to add 2 functions for the class on PreRemove and PostRemove
/**
* #ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->temp = realpath($this->path);
}
/**
* #ORM\PostRemove()
*/
public function removeImg()
{
if (isset($this->temp)) {
unlink($this->temp);
}
}
This functions will be fired: 1st before remove (to store file name) and second after Entity is removed from DB to remove related File.
You need also to define
private $temp;
That stores file name.
That's it. Now when you remove news or news category all related media files will be removed with the it's entity.

Efficient Core Data Recursion

Context
I have a Core Data entity called "LPFile" that represents a file on disk. It has an optional relationship to itself that allows files to "import" each other, like so:
imports<<---->>importedBy
Question
Now, suppose I have this situation with Files 1, 2, 3, and 4:
File 1 is importedBY 2 and 3. Files 2 & 3 are importedBY 4. What I want to know is: if I start at file 1, what's the most efficient approach for finding the "base" or "end" file of this relationship (in this case, that's file 4)? I can write a simple recursive function that looks at each entity in the importedBy relationship, and follows the chain until it finds an entity with zero entities in the importedBy relationship, but I wanted to see if Core Data has a pre-baked method to do this.
Thanks!
Core Data has no pre-baked method to find a root. So your way of looping through it is fine.
Altough this questions has been answered, I solved a similiar problem on a tree made by same entities by adding an attribute called with much fantasy "breadcrumb" and filling that at runtime so that if I have entity model
X {
name NSString
breadcrumb NSString
to-many X relationship
}
A,B,C,D,E like that:
A-->B
-->C-->D
-->E
I end up with this:
A {
breadcrumb /A
relationship B,C,E
}
B {
breadcrumb /A/B
relationship nil
}
C {
breadcrumb /A/C
relationship D
}
D {
breadcrumb /A/C/D
relationship nil
}
E {
breadcrumb /A/D
relationship nil
}
I can say that indexing breadcrumb, make things faster, and I can do regex search.
Important, when I have an entity I can easily find its root without cycling.
Of course I had some mechanism to avoid loop and uniqueness of breadcrumb, based on 'name' attribute.

NHibernate AliasToBeanResultTransformer & Collections

I would like to return a DTO from my data layer which would also contain child collections...such as this:
Audio
- Title
- Description
- Filename
- Tags
- TagName
- Comments
- PersonName
- CommentText
Here is a basic query so far, but i'm not sure how to transform the child collections from my entity to the DTO.
var query = Session.CreateCriteria<Audio>("audio")
.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property<Audio>(x => x.Title))
.Add(Projections.Property<Audio>(x => x.Description))
.Add(Projections.Property<Audio>(x => x.Filename))
).SetResultTransformer(new AliasToBeanResultTransformer(typeof(AudioDto)))
.List<AudioDto>();
Is this even possible, or is there another reccomended way of doing this?
UPDATE:
Just want to add a little more information about my scenario...I want to return a list of Audio items to the currently logged in user along with some associated entities such as tags, comments etc...these are fairly straight forward using MultiQuery / Future.
However, when displaying the audio items to the user, i also want to display 3 other options to the user:
Weather they have added this audio item to their list of favourites
Weather they have given this audio the 'thumbs up'
Weather the logged in user is 'Following' the owner of this audio
Favourites : Audio -> HasMany -> AudioUserFavourites
Thumbs Up : Audio -> HasManyToMany -> UserAccount
Following Owner : Audio -> References -> UserAccount ->
ManyToMany -> UserAccount
Hope this makes sense...if not i'll try and explain again...how can I eager load these extra details for each Audio entity returned...I need all this information in pages of 20 also.
I looked at Batch fetching, but this appears to fetch ALL thumbs ups for each Audio entity, rather than checking if only the logged in user has thumbed it.
Sorry for rambling :-)
Paul
If you want to fetch your Audio objects with both the Tags collection and Comments collections populated, have a look at Aydende Rahien's blog: http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate.
You don't need to use DTOs for this; you can get back a list of Audio with its collections even if the collections are lazily loaded by default. You would create two future queries; the first will fetch Audio joined to Tags, and the second will fetch Audio joined to Comments. It works because by the time the second query result is being processed, the session cache already has the Audio objects in it; NHibernate grabs the Audio from the cache instead of rehydrating it, and then fills in the second collection.
You don't need to use future queries for this; it still works if you just execute the two queries sequentially, but using futures will result in just one round trip to the database, making it faster.