Friday, September 5, 2008

Bridge Design Pattern using C# .Net

Bridge Design Pattern:

[This article has been created from a presentation that was created by my wife-Anita. Special thanks to Anita for facilitating the presentation.]

Definition: Decouple an abstraction from its implementation so that both can vary independently

You will find many articles describing the theory about using Bridge Design Pattern using c# .net. Many articles including wiki has already listed down possible usage of this design pattern. I will simply start with a problem statement and we will see how this problem can be achieved.

Here is the class diagram of my vacation planner (The problem statement) and the code.

Here is the code

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

class Program

{

static void Main(string[] args)

{

TravelManager tm = new TravelManager();

utilize(tm);

FoodManager fm = new FoodManager();

utilize(fm);

//fm.PayBill();

GuideManager gm= new GuideManager();

utilize(gm);

Console.ReadKey();

}

public static void utilize(AgencyManager am)

{

am.Engage();

am.Release();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

abstract class AgencyManager

{

public abstract void Engage();

public abstract void Release();

//public abstract void PayBill();

//public void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

class TravelManager : AgencyManager

{

public override void Engage()

{

TravelController.BookVehicle();

}

public override void Release()

{

TravelController.ReleaseVehivle();

}

//public override void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

class FoodManager : AgencyManager

{

public override void Engage()

{

FoodController.OrderFood();

}

public override void Release()

{

FoodController.FinishFood();

}

}

class GuideManager : AgencyManager

{

public override void Engage()

{

GuideController.HireGuide();

}

public override void Release()

{

GuideController.DeHireGuide();

}

//public override void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

public class TravelController

{

public static void BookVehicle()

{

Console.WriteLine("Vehicle booked");

}

public static void ReleaseVehivle()

{

Console.WriteLine("Vehicle Released");

}

}

public class FoodController

{

public static void OrderFood()

{

Console.WriteLine("Food ordered.");

}

public static void FinishFood()

{

Console.WriteLine("Food finished.");

}

}

public class GuideController

{

public static void HireGuide()

{

Console.WriteLine("Guide hired.");

}

public static void DeHireGuide()

{

Console.WriteLine("Guide dehired.");

}

}

}

As listed down in the above code and class diagram I have a simple abstract class and 3 implementations. Now the problem starts when I start exploring the application more. Let's say I have to add a new method PayBill which are required only for two implementations i.e for TravelManager and GuideManager. However I want to keep the third implementation FoodManager away from this method. Since all of them are implementing the same abstract class, I cannot add this method at the abstract level since in that case it will compulsory for FoodManager to implement that method which I don't want. The other way is to add the method at individual classes. However that will invite unnecessary duplication of code.

There can be someother solution to this problem but we will see how to handle such situation using the Bridge Design Pattern.

So here is the class diagram and the resulting code which shows us how we can resolve this issue.

And here is the code

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

class Program

{

static void Main(string[] args)

{

PaidAgencies agency = new PaidAgencies();

agency.AgencyManager = new TravelManager();

agency.Engage();

agency.Release();

agency.PayBill();

agency.AgencyManager = new GuideManager();

agency.Engage();

agency.Release();

agency.PayBill();

AddOnAgecies freeagency = new AddOnAgecies();

freeagency.AgencyManager = new FoodManager();

freeagency.Engage();

freeagency.Release();

freeagency.Reimburse();

Console.ReadKey();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

/// Abstraction

/// </summary>

public abstract class AgencyAbstraction

{

AgencyManager agencyManager;

public AgencyManager AgencyManager

{

get { return agencyManager; }

set { agencyManager = value; }

}

public abstract void Engage();

public abstract void Release();

}

/// <summary>

/// Refined abstraction

/// </summary>

public class PaidAgencies : AgencyAbstraction

{

public override void Engage()

{

AgencyManager.Engage();

}

public override void Release()

{

AgencyManager.Release();

}

public void PayBill()

{

Console.WriteLine("Amount Debited to Account");

}

}

/// <summary>

/// Refine abstraction 2

/// </summary>

public class AddOnAgecies : AgencyAbstraction

{

public override void Engage()

{

AgencyManager.Engage();

}

public override void Release()

{

AgencyManager.Release();

}

public void Reimburse()

{

Console.WriteLine("Amount credited to Account");

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

/// This is implementor

/// </summary>

public abstract class AgencyManager

{

public abstract void Engage();

public abstract void Release();

}

/// <summary>

/// This is concrete implementor

/// </summary>

public class TravelManager : AgencyManager

{

public override void Engage()

{

TravelController.BookVehicle();

}

public override void Release()

{

TravelController.ReleaseVehicle();

}

}

/// <summary>

/// This is concrete implementor

/// </summary>

class FoodManager : AgencyManager

{

public override void Engage()

{

FoodController.OrderFood();

}

public override void Release()

{

FoodController.FinishFood();

}

}

/// <summary>

/// This is concrete implementor

/// </summary>

class GuideManager : AgencyManager

{

public override void Engage()

{

GuideController.HireGuide();

}

public override void Release()

{

GuideController.DeHireGuide();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

///

/// </summary>

public class TravelController

{

public static void BookVehicle()

{

Console.WriteLine("Vehicle booked");

}

public static void ReleaseVehicle()

{

Console.WriteLine("Vehicle Released");

}

}

public class FoodController

{

public static void OrderFood()

{

Console.WriteLine("Food ordered");

}

public static void FinishFood()

{

Console.WriteLine("Food finished");

}

}

public class GuideController

{

public static void HireGuide()

{

Console.WriteLine("Guide hired");

}

public static void DeHireGuide()

{

Console.WriteLine("Guide dehired");

}

}

}

As you can see now we can add methods at the Abstraction level whereas clients are using the refined abstraction. This way we are free to make changes at the abstract level as well as at the implementation level.

I Hope this explains the bridge design pattern clearly. Please feel free to comment on this article in case you have any queries/suggestions/comments. I have this source code along with a presentation in a zip and I can provide the same free of cost. Email me at SubhashD987[AT]mail.com

Enjoy programming…

Friday, August 8, 2008

Create new folder, Upload document to sharepoint site – Maintain metadata

There was one requirement which I was working on recently. It was very simple to create a user control wherein the user should be able to select the file and upload it to a WSS 3.0 Document Library. Also the second part was user should be able to create folders in the document library and upload document inside the folder.

 

With this very simple task was stated to me, I was very happy to answer my boss on the spot that the, I have the design ready. But boss (being a boss !) told me, Hold On! The requirement also states that the user should be able to see his/her name after uploading document and creating folder. For this I had a very straight forward solution which is inbuild for WSS 3.0. I used following code to upload the document.

    /// <summary>

/// Uploads Document to the Site

    /// foldername: Name of folder to which the document is to be uploaded. (Eg. "Shared Documents")

    /// documentFIleName: FileName of the file to be uploaded. Typically FileUpload control's output after formatting gets in here (Eg. "C:\MyFile.Doc")

/// </summary>

public void UploadDocumentToSite(string foldername, string documentFileName)

{

SPSite site = SPControl.GetContextSite(HttpContext.Current);

SPWeb myWeb = site.AllWebs["123"]; //123 is the site id.

 

try

{

site.AllowUnsafeUpdates = true;

myWeb.AllowUnsafeUpdates = true;

 

SPFolder destFolder = myWeb.GetFolder(foldername);

SPUser user = myWeb.Users["Domain\\UserName"];

 

destFolder.Files.Add(documentFileName, content, user, user, DateTime.Now, DateTime.Now);

         //The above overload used will help me to maintain the metadata such as created by and modified by fields.

 

}

catch (Exception ex)

{

//Log the error here

}

finally

{

//Dispose the web and site

}

}

 

So far so good. The above code perfectly uploads the document to the document library and also maintains the metadata such as Created by/Modified By fields.

The challenge starts later. Now the same requirement had a provision that user should be able to create folders in the document library. Creating folder is simple

SPFolder destFolder = myWeb.GetFolder(foldername);

destFolder.SubFolders.Add(folderName);

Now the problem starts. The above SubFolders.Add method does not have any overload where I can pass on SPUser. Apparently what would happen is that the document gets uploaded with some other userid (or system account which was impersonated) but not with the specific userid I wanted.

Then I again searched on the web and somewhere down the line I got a hint which worked for me like anything. The below is the way to go about it.

SPFolder newFolder = destFolder.SubFolders.Add(folderName);

newFolder.Item["Modified By"] = user.ID;

newFolder.Item["Created By"] = user.ID;

newFolder.Item.Update();

 

Wow… This was so simple but still so important to know.. I am very thankful to the guy who floated this on the web. (Unfortunately I did not bookmarked the url, but I commented down there)..

Go ahead and try this out if this works for you.. if it does, keep the work spreading…

Enjoy Life .. Enjoy Programming.. ..

Let me know if you need any assistance on such issues…

Friday, July 4, 2008

Issue with Document upload in WSS 3.0 migrated site

This time I have one problem with which I have been struggling a lot but still I do not have any solution. If any one of you have the solution, please let me know.

Problem Statement:

The custom document library schema.xml is not being utilized completely in case of upload action.

 

Details:

We have migrated our custom site from WSS 2.0 to WSS 3.0. We have a custom document library and it was also part of the migration. After migration we have used "UseLegacyForm" attribute in all of our custom list/document libraries' schema.xml. Using this attribute we are able to keep our exists lookup and other functionalities unaffected. However we observed that in case of custom document library, when we try to request for "Upload Document" functionality in the service. The call is getting redirected to _layouts/Upload.aspx instead of Forms/Upload.aspx. That means that the content of our schema.xml is not being followed. Surprisingly the forms/upload.aspx page is invoked properly if we are creating a "New Folder". But whenever we try to upload the document the requested gets redirected to _Layouts/Upload.aspx instead of Doclib/Forms/Upload.aspx.

 

Wednesday, May 14, 2008

Vayam call for urgent action

This is not technical but serious social issue in India. I am quoting email below from Milind Thatte of Vayam India. Please see if any one can help here!!. You may ask me [Subhash Dike] if you need any further details. Visit http://vayamindia.wordpress.com/ for details "Vayam call for urgent action Dear all, I am writing this to involve all of you in a war-footing effort that we alias Vayam are taking up in this month and the next. This is almost a war and Vayam has to fight it. We can not be bystanders and watch our forests and the communities there just vanish into deserts of barren mountains. The calamity and opportunity Government of India has passed a law that is handing over "Forest rights" to forest-dwelling communities. This law is a double-edged sword. If the community (i.e. the tribes) use it in a proper and restrained manner, it will rejuvenate all of our forests. But if the community falls prey to campaigns by mafia, political parties, and some NGOs - the forests will vanish within next six months. As of now, there is double tragedy: 1 - the community does not know how to manage forests for long-term sustainability and benefit. 2 - the anti-forest lobby is already at large Our war plan There is need to launch an awareness campaign with immediate effect (starting latest by 20th May) in all forest-dwelling villages of Jawhar-Mokhada blocks (Thane district). We have identified that 24 villages in this belt are still having some forest cover. Our team will reach these villages, conduct meetings, present a workable Forest Management model, and appeal people to join our movement. Our movement shall support both: Forest rights and Forest management by community. This campaign is proposed to be run from 20th May to 15th June. Please understand the urgency. Government has begun the implementation of this law and is bent upon completing the procedures in six months beginning April 2008. Village forest committees are already formed in most villages... and forest maps will be redrawn in next month. What YOU got to do You can volunteer for this campaign, but not for less than seven days. If you can write a streetplay (in tribal dialect), or help design/print posters, or if you can drive a car, hold a loudspeaker, fold a chaddar, take photographs, or if you can talk to villagers... You are welcome to volunteer. We need approx. 1,50,000 rupees to run this campaign. If you are not coming to the field, you have to raise this fund and empower the field team to work without worry. Some of the expenditures are as follows: 1. Printing of leaflets/handbills, posters, and stickers 2. Printing of Flex (roll-up boards) for temporary exhibition in villages 3. Vehicle hiring expenses (or fuel for motorcycles) {You can donate a motorcycle for a month} 4. Honorarium for field workers (working for one month or more) 5. Food expenses of the campaign team 6. White LED torches (10) to illuminate the stage for night meetings We have about 42k rupees contributed by our core team. (We have another 28k by donors, but we will have to seek their permission to use it for this purpose.) So we need more funds. You can drop a cheque in the name of "Amit Tillu" or "Milind Thatte" and drop it in any ICICI ATM. (Account number: 002701551755, Branch: Nashik). If you (or your friend) donates, please send a mail to vayamindia@gmail.com and give your contact details. All contributors will get a detailed report of expenses at the end of the campaign by email. I am not going to send reminders. This is not my war, this is our fight. And we don't have too much time in hand. I am going to be in the field for this entire campaign. Two tribal karyakartas are ready to be with me. Many more will join. Even if you don't collect funds, this team will fight. But then, their families will suffer - as all of them will lose their monthly/daily wages. Feel free to fwd this mail to your friends and talk to as many people as possible.(Vayam's profile - if required - is attached.) Your contributions are can start falling in from today. Call me for all queries/questions. Ram ram, - Milind More information about this issue... What is the law all about This law and its rules were passed on 1 January 2008 and it is called as "Scheduled Tribes and Other traditional Forest-dwellers (Recognition of Forest Rights) Act". The Act gives or recognises three sets of rights: 1. Right to own/cultivate/habitat land in forest for bonafide livelihood needs. (Cultivation/habitation before 2005 is to be legalised) 2. Right to use/distribute/process all plant-based forest produce (excluding timber) and to use all grasslands and water sources in the forest for bonafide livelihood needs 3. Right to conserve/manage forest resources The law is positive in the sense that it has recognized the rights of tribes over forests that were taken away by the British government and carried forward by Indian governments. But it is dangerous - for it is quite possible - that people will destroy/cut forests to get immediate gains. Deforestation will damage the forest-dwellers to the maximum. Food-fuel-fodder everything will be lost and forest-dwelling communities will virtually become beggars if forests are finished. Assessment visit (cutting of forests has begun) Vayam's team has already been to some forest villages in Thane districts and assessed the current status of the law and the forests. Village Forest committees - as required by the law - are being formed. In some villages, certain organisations have provoked people to cut forests and we have seen cut trees (approx. 90 in one village, Bharsatmet in Taluka Jawhar). We met some forest farming persons and some Gram Panchayat Forest committee members. (See pictures attached.) After this assessment, we have chalked out the plan of a blitzkrieg campaign. Networking to increase impact We have been talking to Voluntary organisations with good grass-roots network. I and Amit have already alerted all our old contacts and friends in tribal villages and have got them activated. We have addressed two meetings of tribal volunteers and are connecting to friends in MP and 36gadh. "

Monday, April 21, 2008

0x81070201 error

Recently I was going through a critical issue. I used to gets 0x81070201 error when I open default page of my sharepoint site in WSS 3.0. While I was looking for solution one of the blogs gave me great relief. The issue may appear because Schema.xml files from site definition folder under sitetemplates on webserver or schema tag is missing from onet.xml. If you simply reload these files at site definition and listdefinition, your problem should be resolved. [While I was searching around for the similar issue, I observed that similar problem may occur if your schema.xml and/or onet.xml is not accessible] Thanks, Subhash

Monday, January 21, 2008

SPBasePermission

While my friends were having fun during weekends, I was hitting my head against an issue in my application. It took me a very long time to understand and resolve the issue. But finally all well that ends well. Here is the issue. I use Visual Studio for creating a subsite under a site collection. The users are present in the site collection under "Read" role. I use impersonation to create the subsite. After creation of site, I create RoleDefinition and the RoleAssigment so that the users have all appropriate rights on the newly created subsite. I was properly breaking inheritance by using web.RoleDefinitions.BreakInheritance(false,false); The above statment was written to ensure that the "Read" role permissions are not carried over to subsite, instead subsite should use their unique permission level and unique role definition. The problem was, after creation of subsite the subsite was not accessible to the users in the specified group. Inspite of all above written measures, I was not sure what is causing this error. After trying lots of things finally I got the solution. The solution was simple as to have following permissions applied to the RoleDefinition at the time of creation SPBasePermissions.ViewListItems SPBasePermissions.AddListItems SPBasePermissions.EditListItems SPBasePermissions.DeleteListItems SPBasePermissions.OpenItems SPBasePermissions.ViewVersions SPBasePermissions.DeleteVersions SPBasePermissions.ViewFormPages SPBasePermissions.Open SPBasePermissions.ViewPages SPBasePermissions.BrowseDirectories SPBasePermissions.BrowseUserInfo SPBasePermissions.UseClientIntegration SPBasePermissions.UseRemoteAPIs SPBasePermissions.CreateAlerts SPBasePermissions.EditMyUserInfo; That resolved my issue. If anybody has same or similar issue, let me know. I would be happy to assist. Thanks, Subhash

Sunday, January 20, 2008

Remember this while upgrading from WSS 2.0 to WSS3.0

In this article we would try to figure out few small things which you must remember before/during an In-place upgrade from WSS 2.0 to WSS 3.0. 1. Connection strings/ connection string information: If you have a custom application running under a sharepoint site, make sure that you do not have connection string like "Data Source=(local);Initial Catalog = MyDB;....." etc. This is because the upgrade might fail because of the word (local). Same is applicable to your settings in Sharepoint Central Administration. Make sure that Content/configuration Database server name is not (local) or . Even if you are using local server as database server, make sure you give Full Name. 2. Prescan-tool: After the last step of Installation of WSS 3.0, the installation wizard have one check-box for executing Sharepoint Technology Configuration Wizard. This check-box is checked by default. However the upgrade would fail if you continue as is. Ensure that you have executed Pre-scan tool before execution Sharepoint Technology and configuration wizard. The prescan tool exists under programfiles/Microsoft Shared/Web server extension/12/bin directory. 3. Rights: Make sure that the service accounts are set properly and that these accounts have all required access to all servers and to the database. In the unfortunate even of upgrade failure, don't worry. Just visit upgrade log available under programfiles/Microsoft Shared/Web server extension/12/logs. Understand the exception and act accordingly. Though wizard states that the process is irriversible, you can always correct the issues and execute the wizard again. In most of the cases, the upgrade fails because of few obvious reaons. Enjoy !! Subhash

Saturday, January 12, 2008

System policies must have full control !!

I have been trying hard with a problem with the search service. Here is the problem definition. While using Windows Sharepoint Services 3.0 Search Service, when you try to start the search service instance on webserver, it throws error "System policies must have full control". And this is the solution that worked for me: I digged into all kind of books, technical references. There may be several different reasons behind this error and solutions defer from situation to situation. But the in my case problem was with the Service Account and Content Access account. I was trying to start the search service using credentials of Farm Administator. However it has been explicity mentioned on Microsoft Techcenter, that the content access account and service account MUST NOT BE farm administrator. I got this reference from here: http://technet2.microsoft.com/Office/en-us/library/f07768d4-ca37-447a-a056-1a67d93ef5401033.mspx?mfr=true . I just created a seperate domain account and used that as service & content access account. That's it!!! The search service started working very smoothly. Hope this helps you. Happy programming :-) [Please note: This solution/comment is based on personal experience and may not be authentic. This solution does not guarantee that it would react the same way anywhere else. You can refer to product documentation and authorized websites for more information.]