Sunday, October 20, 2013

The "REACH" initiative

After visiting some childrens' homes, elders' homes and some hospitals it was evident to me that most of those places are beyond reach of the generous donors. Even those that are within reach they had requirements and needs that the donors aren't even aware of. There are so many people who are willing to supply tangibles and monetary support but it all goes only to a privileged few and most social service institutes are well forgotten. However there is some form of listing maintained in ministry of social service of all the locations but their needs are different and need to be identified and helped accordingly.

Also some institutions need help continuously so the donors need to know the need properly as they are also in two categories. One is that who donate once to commemorate some event and those who look out to support continuously. Therefore a proper platform needs to be in place to identify the required and providers and sync them together.

The "REACH" initiative aims to achieve this goal and provide a platform for the needy and the provider. As members of "REACH", we visit places of need and find out their requirements. The we post about them in our non profit website. The donors can visit the site and find institutes that matches their donation or they can donate what is specified to be the needs of the institutions.

Also donors and institutions can reach us via the hotline and register for out service. Then we will check the validity of the provided information and post them in our site.

Currently this is in conceptual state and we will post here when we go live. For further information contact us at 0777-731716.

Saturday, July 13, 2013

Implementing the tree hierarchy using TreeView

I've just completed the long run of implementing the classes required for a hierarchical display using the "TreeView" control in asp.net and felt like sharing for the convenience of some who would come across the need someday :) I know that there are a lot of Jquery controls that does this without much hassle, however I find it much more controllable to implement using the back-end.

Refer this, where the relationship of Categories are converted to the tree structure. Note that this design only works for a single parent multiple children hierarchy. A multiple parent structure would be a complete different story.


The fact that differentiate TreeView from other collection views is that it expects a HierarchicalDataSource as its data source type. Therefore we will have to custom build the data retrieval and methods.
I'll use the naming convention prefixing "Category" for my classes as it suits my purpose.
We'll have to implement these 4 classes.

1. CategoryTreeNode.cs - Represents a single node in the tree hierarchy.
2. CategoryTree.cs - Represents a collection of nodes under a parent node.
3. CategoryTreeDataSourceView.cs - Builds the structure for the provided root node.
4. CategoryTreeDataSource.cs - Acts as the data source for the TreeView control.

CategoryTreeDataSource initializes with a populated root node using NodeLists and Nodes. We'll have to build this beforehand and then feed the root node to the data source. A recursive method like below can be used to populate the root node.

public static void PopulateCategoryTreeNodeWithHierarchy(CategoryTreeNode node)
{
if (node != null)
{
//Use your own data retrieval method.
                IList<Category> categories = AssistantFactory.SummonAssistant<CategoryAssistant>  ().SelectChildren(node.Category.Id);
foreach (Category category in categories)
{
CategoryTreeNode treeNode = new CategoryTreeNode(category);
treeNode.Parent = node;
node.ChildCategories.Add(treeNode);
}

if (node.ChildCategories != null && node.ChildCategories.Count > 0)
{
foreach (CategoryTreeNode childNode in node.ChildCategories)
{
PopulateCategoryTreeNodeWithHierarchy(childNode);
}
}
}
}

CategoryTreeDataSource inherits "HierarchicalDataSourceControl" and "IHierarchicalDataSource". Since it inherits the interface "IHierarchicalDataSource" you will have to override its required method "HierarchicalDataSourceView GetHierarchicalView(string viewPath)" which returns an instance of "CategoryTreeDataSourceView".

CategoryTreeDataSourceView inherits "HierarchicalDataSourceView" which holds and returns nodes of the tree hierarchy.

CategoryTreeNode inherits "IHierarchyData" which requires you to implement below methods and props,
bool HasChildren { get; }
object Item { get; }
string Path { get; }
string Type { get; }
IHierarchicalEnumerable GetChildren();
IHierarchyData GetParent();

Most methods and props are self explanatory. The most important prop of these is the "Path" which specifies the path of a node down the hierarchy and needs to be setup carefully.

CategoryTree holds a collection of CategoryTreeNodes in a list and returns a converted IHierarchicalEnumerable required by the data source.
 This is just a high level reference for the implementation. I've added the classes below for a clearer reference. Also you can visit the MSDN reference here for a more technical overview.

Cheers!

public class CategoryTreeDataSource : HierarchicalDataSourceControl, IHierarchicalDataSource
{
public CategoryTreeDataSourceView DataSourceView { get; set; }
public CategoryTreeNode RootNode { get; set; }

public CategoryTreeDataSource(CategoryTreeNode root) : base()
{
RootNode = root;
}

protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
{
if (DataSourceView == null)
{
DataSourceView = new CategoryTreeDataSourceView(RootNode);
}

return DataSourceView;
}

protected override ControlCollection CreateControlCollection()
{
return new ControlCollection(this);
}
}

public class CategoryTreeDataSourceView : HierarchicalDataSourceView
{
public CategoryTreeNode RootNode { get; set; }
public string ViewPath { get; set; }

public CategoryTreeDataSourceView(string path)
{
ViewPath = path;
}

public CategoryTreeDataSourceView(CategoryTreeNode root)
{
RootNode = root;
}

public override IHierarchicalEnumerable Select()
{
CategoryTree children = new CategoryTree();
if (RootNode != null && RootNode.ChildCategories != null)
{
foreach (CategoryTreeNode child in RootNode.ChildCategories)
{
children.Add(child);
}
}

return children;
}
}

public class CategoryTree : List<CategoryTreeNode>, IHierarchicalEnumerable
{
public IList<CategoryTreeNode> Categories { get; set; }

public CategoryTree() : base()
{
Categories = new List<CategoryTreeNode>();
}

public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as CategoryTreeNode;
}
}

public class CategoryTreeNode : IHierarchyData
{
public Category Category { get; set; }
public IList<CategoryTreeNode> ChildCategories { get; set; }
public CategoryTreeNode Parent { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }

public CategoryTreeNode(Category category)
{
Category = category;
Name = category.Name;
CategoryId = category.Id;
ChildCategories = new List<CategoryTreeNode>();
}

public static CategoryTreeNode GetRootCategoryTreeNode()
{
CategoryTreeNode node = new CategoryTreeNode(AssistantFactory.SummonAssistant<CategoryAssistant>().SelectRoot());
return node;
}

public static void PopulateCategoryTreeNodeWithHierarchy(CategoryTreeNode node)
{
if (node != null)
{
IList<Category> categories = AssistantFactory.SummonAssistant<CategoryAssistant>().SelectChildren(node.Category.Id);
foreach (Category category in categories)
{
CategoryTreeNode treeNode = new CategoryTreeNode(category);
treeNode.Parent = node;
node.ChildCategories.Add(treeNode);
}

if (node.ChildCategories != null && node.ChildCategories.Count > 0)
{
foreach (CategoryTreeNode childNode in node.ChildCategories)
{
PopulateCategoryTreeNodeWithHierarchy(childNode);
}
}
}
}

public IHierarchicalEnumerable GetChildren()
{
CategoryTree children = new CategoryTree();
foreach (CategoryTreeNode child in ChildCategories)
{
children.Add(child);
}

return children;
}

public IHierarchyData GetParent()
{
CategoryTreeNode parent = new CategoryTreeNode(AssistantFactory.SummonAssistant<CategoryAssistant>().Select(Category.ParentCategoryId));
return parent;
}

public bool HasChildren
{
get
{
return (ChildCategories != null && ChildCategories.Count > 0);
}
}

public object Item
{
get
{
return this;
}
}

public string Type
{
get
{
return this.GetType().ToString();
}
}

public string Path
{
get
{
CategoryTreeNode node = this;
string valuePath = String.Empty;
while ((node = node.Parent) != null)
{
valuePath = String.Concat(node.Category.Id.ToString(), "/", valuePath);
}

return String.Concat(valuePath, this.Category.Id.ToString());
}

}




Monday, July 1, 2013

Lounge - A market gone further

Many years ago the technology and commerce subjects we learnt always had a common conclusion; where we will be in the coming years? In Sri Lanka of course the mass market was exposed to ecommerce in early 2000 when prices and infrastructural state became affordable for everyone. Now we can say that we are in par with the globe with respect to the availability and access to ecommerce. The business of ecommerce is for everyone given the ease of setup and the minimal overhead and cost if they fail. Thus we see a lot of initiatives coming out, some successful and some failures. Not to mention 'lounge' - which is just another initiative put forth with the probability of both success and failure.
An internet marketplace, a very popular ecommerce concept and widely seen in many formats everywhere, success depends on the services they offer and how those will be useful for the people who looks forward to visit a marketplace for their different needs.
A very popular and most related example would be Amazon, eBay and anything.lk, the latter being a Sri Lankan success in its own context. They all are marketplaces with different approaches on how they would cater to their customer needs. Amazon, a department based marketplace, bahaves a shopping complex where different vendors can sell their content through features exposed to them. EBay on the other hand uses the auction concept where they let individuals to post items and others to bid for them. They also expose features related to auction ecommerce such as auto bidding. Anything.lk, our local company uses a modern concept called mass selling format where they post an item and it should reach a selected number of sales to be available. With this concept, these goods can be sold with a considerable discount as the quantity is always higher than normal.
These are a few of very successful companies who practiced interesting ecommerce concepts and manipulated them strategically. However I believe there are still so many new concepts out there, some which can be done by modifying existing and commonly practiced concepts. It always depends on what customer wants (or in pursuading the customer that he wants it) and properly addressing those requirements would be the key and a little bit of marketing. Customer is the king in this and the first impression always matters to get it bookmarked in his browser. The user traffic is the most important factor.

Thursday, February 7, 2013

Core data lazy loading

Apple's explanation on Core data lazy loading.

When you execute a fetch, Core Data fetches just instances of the entity you specify. In some situations, the destination of a relationship is represented by a fault. Core Data automatically resolves (fires) the fault when you access data in the fault. This lazy loading of the related objects is much better for memory use, and much faster for fetching objects related to rarely used (or very large) objects. It can also, however, lead to a situation where Core Data executes separate fetch requests for a number of individual objects, which incurs a comparatively high overhead.

Source - https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html

How to see raw sql in core data execution

In XCode, go to scheme list and select edit scheme. Pick 'Run' scheme and select arguments.
In 'Arguments passed on launch' add the following “-com.apple.CoreData.SQLDebug 1”.
Now when you run the app you will see the sql execution status in the output window.

Source - http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started