Create Item

Interface

Remember that an Item carries properties and that every property is identified by its property ID. Lets define these properties in an interface, for the flickr-simple-browser we will use SimpleFlickrItem. Note that it extends BasicFlickrItem which resides in the module flickr-integration which is the basic item interface to be extended by other submodules. SimpleFlickrItem has no additional properties.

info.magnolia.flickr.simplebrowser.app.item.SimpleFlickrItem
public interface SimpleFlickrItem extends BasicFlickrItem {

}
info.magnolia.flickr.app.item.BasicFlickrItem
public interface BasicFlickrItem extends Item {
    public static String PROPERTY_TITLE = "title";
    public static String PROPERTY_DESCRIPTION = "description";
    public static String PROPERTY_PHOTOID = "photoId";


    public class IDs {
        private static Map<String, Class> properties = new HashMap<String, Class>();
        static {
            properties.put(PROPERTY_TITLE, String.class);
            properties.put(PROPERTY_DESCRIPTION, String.class);
            properties.put(PROPERTY_PHOTOID, String.class);
        }
        public static Map<String, Class> getIDs() {
            return properties;
        }
    }
}

Our interface has the following property IDs:

  • photoId : The ID of the photo given by Flickr.
  • title : The title given to the photo; if it's null we will assign the value of the photoId.
  • description : The description of the photo; is very often null.

The interface contains also a static class which provides a map with all the properties and its types. The map can be achieved by:

Map Map<String, Class> properties = SimpleFlickrItem.IDs.getIDs();

Item utility

We will use 2 different container for the 2 views, hence we cannot couple container and ContentConnector.
In both Container and ContentConnector we will use methods to fetch Item by a given ItemId and vice versa. For this reason we implement a utility class which provides these methods to use it in both Container and ContentConnector

info.magnolia.flickr.browser.app.item.FlickrItemUtil
public class FlickrItemUtil {


    public static FlickrItemId createIdByItem(FlickrItem item, FlickrService flickrService) {
        if (item instanceof FlickrItem) {
            String uuid = item.getUuid();
            // album
            if (FlickrItem.FLICKRITEMTYPE_ALBUM.equals(item.getItemProperty(FlickrItem.PROPERTY_FLICKR_ITEM_TYPE).getValue())) {
                return new FlickrItemIdImpl(uuid, false, null);
            }
            // photo
            else {
                return new FlickrItemIdImpl(uuid, true, flickrService);
            }
        }
        return null;
    }


    public static FlickrItem createItemById(FlickrItemId itemId, FlickrService flickrService) {
        if (itemId != null &amp;&amp; itemId instanceof FlickrItemId) {
            String uuid = itemId.getUuid();
            if (FlickrItemId.UuidParser.idValidUrlFragment(uuid)) {
                // Album
                if (itemId.isForAlbum()) {
                    String albumId = itemId.getPhotosetId();
                    Photoset photoset = flickrService.getPhotoset(albumId);
                    if (photoset != null) {
                        return createItemByPhotoSet(photoset);
                    }
                }
                // Photo
                else {
                    String photoId = itemId.getPhotoId();
                    Photo photo = flickrService.getPhoto(photoId);
                    if (photo != null) {
                        return createItemByPhoto(photo);
                    }
                }
            }
        }
        return null;
    }


    public static FlickrItem createItemByPhoto(Photo photo) {
        return new FlickrPropertysetItem(photo);
    }


    public static FlickrItem createItemByPhotoWithParent(FlickrItemId itemId, Photo photo) {
        return new FlickrPropertysetItem(photo, itemId.getUuid());
    }


    public static FlickrItem createItemByPhotoSet(Photoset photoset) {
        return new FlickrPropertysetItem(photoset);
    }


    public static FlickrItemId createIdByPhotoset(Photoset photoSet) {
        return new FlickrItemIdImpl(photoSet);
    }


    public static FlickrItemId createIdByPhotoWithParent(FlickrItemId parentItemId, Photo photo) {
        return new FlickrItemIdImpl(photo, parentItemId);
    }


    public static FlickrItemId createIdByPhotoNoParent(Photo photo) {
        return new FlickrItemIdImpl(photo);
    }

 public static FlickrItemId createIdByUrlFragment(String urlFragment, FlickrService flickrService, boolean addParent) {
        return new FlickrItemIdImpl(urlFragment, addParent, flickrService);
    }
}

 

Bla bla bla ...

Tabs tabs ...

what a cool thing ...

 

 

 

  • No labels