Sunday, June 14, 2009

HTML Viewer and MOSS 2007

Some of you already know that SharePoint 2003 (and WSS v2) provide a functionnality which convert Office document on HTML page for client which has not Office installed.
This function is called HTML Viewer and need to be downloaded as an add-on.
Now, if you check on MOSS/WSS v3, you still have the capablity to manage this function (like in SPS 2003). However, the download for HTML Viewer currently available DOESN'T WORK with MOSS/WSS v3.
Mainly, this is due to some security issue found after the realease and for some other part for the new code used by MOSS.
Actually, the product group doesn't plan to release a newer version of HTML Viewer which can works with MOSS. Seamily, due to a very low usage (counted thanks to the number of download for HTML Viewer) of HTML Viewer in SPS 2003.
So, if you plan to use this functionnality, you have to wait for third party or news for the product group.

Monday, March 30, 2009

Implement the Object Model in a Custom Web Part

How to: Implement the Object Model in a Custom Web Part

http://msdn.microsoft.com/en-us/library/ms479215.aspx

Working with List Objects and Collections

http://msdn.microsoft.com/en-us/library/ms460897.aspx

ERROR

The "SimpleWebPart" Web Part appears to be causing a problem. Updates are currently disallowed on GET requests.  To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb

SOLUTION

SPWeb myweb2 = SPControl.GetContextWeb(Context);

myweb2.AllowUnsafeUpdates = true;


ERROR

Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security

SOLUTION

<trust level="Full" originUrl="" />

To get the PublicKeyToken

C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin>

sn -T *.dll

Where to Put Webparts

C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin

The web.config you have to edit to add safeControls

MOSS 2007 Theme

MOSS 2007 Theme

- Copy
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\THEMES\GRANITE folder

--- Theme.inf Title and theme information
--- Theme.css
--- Images

- Rename GRANITE.INF with ANYTHING_THEME.INF
Open the file and replace every GRANITE word with ANYTHING_THEME

- Open this file with notepad
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\SPTHEMES.XML

- Add this entry:

ANYTHING_THEME

ANYTHING_THEME

ANYTHING_THEME bla bla bla.

images/thANYTHING_THEME.gif

images/thANYTHING_THEME.gif

MOSS 2007 Export/Import

MOSS 2007 Export/Import

- This task can be done only locally on the server machine.
- Use the user that been used for installing MOSS.
- Open the command line.
- Go to this location:
\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\

Export command:
stsadm.exe -o export -url http://SERVER_NAME -filename c:\FILE_NAME.bak -overwrite

Import Command:
stsadm.exe –o import –url http://URL –filename c:\FILE_NAME.bak

NOTE:
If you got this error: Object reference not set to an instance of an object.
You are not the administrator, login using the user that been used to install MOSS

The two sites should be the same, make sure that they have the same features. Ex: Publishing site can't be imported to team Site.

NHibernate and Sharepoint 2007

NHibernate and Sharepoint 2007

Sharepoint webpart that uses NHibernate to do Database operations.
It was a hard task, spent 2 days fixing deployment errors and assemblies dependencies.

At the end it worked :)

Make sure:
- Hibernate mapping file build action is embedded resource
- Use NHibernate.ByteCode.Castle not NHibernate.ByteCode.LinFu (I donno why!)
- hibernate.cfg.xml and NHibernate.ByteCode.Castle.dll should be in C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin
- NHibernate required libraries put them in the GAC C:\WINDOWS\assembly

Tuesday, July 24, 2007

Hibernate Querying Optimization - HQL with joins

The problem:


For each object that Hibernate loads, it needs to do one or more extra SQL queries to load associated objects. (Potentially as many as Kn + 1, where n is the number of objects returned).

Example:




  • Simplified hibernate mapping file

    <class name="Department">




    <many-to-one name="employee".../>




    </class>




  • Simple HQL Query

    from Department dept where dept.location like :LOCATION



  • The generated SQL statements:




  1. At least the initial select.

    select dept_id, deplt_name, dept_location...from department...




  2. Then a sequence of selects from the associated table. (n sql select statements)

    Select ...from employee...


    Select ...from employee...


    Select ...from employee...


    ...


    If Sale table returns 100 rows, you will see 100 select statements extra more than the main select statement.


The solution (1):


The association objects must be loaded in the initial query and just when you need them. This can be done by adding 'left join fetch' for example and switching the lazy loading on.

from Department dept left join fetch dept.employee


where dept.location like :LOCATION


This will generate just one sql select statement.

The solution (2): When there are a lot of association tables


You don't want to end up with query like this:

from Department dept


left join fetch dept.employee


left join fetch dept.manager


left join fetch dept.building


left join fetch dept.level


left join fetch dept.city


…etc


The approach involves determining exactly which columns you really need, and instantiating data-transfer objects containing exactly those columns.

Select new Department (dept.id, dept.employee.name, dept.manager.name, …) from Department dept




This technique is fast and efficient, and avoids the overheads of handling associated objects and of loading large numbers of persistent objects into the Hibernate session cache. The only downside is the need to create a dedicated data-transfer class for each query, and an overloaded constructor.



Reference: http://www.javalobby.org/articles/hibernate-query-101/

Inheritance Relation in Hibernate - Multiple join load problem

The Problem

Suppose we have class User which is the super class for all kind of Users, and we have these subclasses that inherit from User class:

ü EmployeeUser

ü StudentUser

ü TeacherUser

ü MerchantUser

ü VoucherUser

The User class is NOT abstract class, and many methods are implemented in it… keep in mind J

How to present this in Hibernate?

All of them will be presented in User.hbm.xml like this:

<class name="...User">

<id name="id"/>

<property ...

... All the properties in User class

<joined-subclass name="EmployeeUser">

<key column="ID" />

<property ...

... All the properties in EmployeeUser class

joined-subclass>

...All the other subclasses

Simple!!

After running the application, Hibernate will create the database schema... Nothing new Sami!!?? I know J

Let’s say now you want to implement the Login action for example, userName and password is in User class

What you will do is something like this:

//Code to check username and password that user entered in the login screen

Session session =

getSession();

Integer count = (Integer) session.createQuery("select count(*) from User where username = :NAME and password = :PASSWORD").setString("NAME", userName).setString("PASSWORD", password).uniqueResult();

Don’t tell me Sami that the login action is just 6 lines, impossible L

After doing this you will go to your team leader or Manager and tell him that you have done the project and you are ready to be team leader… Just Joking J

But unfortunately, the code you have written is a disaster… Yes, and you are fired out your company L

Look at the Hibernate generated query, you will see a very big query that you haven’t ever seen in your life, and it will be like this:

select count (*) from USER

inner join EMPLOYEE ON USER.ID= EMPLOYEE.ID-----------------1

inner join STUDENT ON USER.ID= STUDENT.ID-------------------2

inner join TEACHER ON USER.ID= TEACHER.ID-------------------3

inner join MERCHANT ON USER.ID= MERCHANT.ID-----------------4

inner join VOUCHER ON USER.ID= VOUCHER.ID-------------------5

Five inner joins for checking username and password that stored in the USER table... What you were thinking about when you wrote this?

This is a big problem, because if three users for example login at the same time, a very huge database load will fly!!

Solution

Using <discriminator> Hibernate tag

You can tell Hibernate that when you want a data for any subclass stored in the superclass, don’t “YA” Hibernate make any extra join that I don’t need, in other words: get the subclass from the superclass.

You will not change anything in your java code; all the changes will be in User.hbm.xml

Change hibernate configuration file to something like this after we take the User.hbm.xml for our example:

<class name="...User">

<id name="id"/>

<property ...

... All the properties in User class

<discriminator column="USER_TYPE" type="string" length="15" not-null="false" force="true"/>

<subclass name="EmployeeUser" discriminator-value="EMPLOYEE">

<join table="EMPLOYEE_USER" fetch="select">

<key column="ID" />

<property ...

... All the properties in EmployeeUser class

join>

subclass>

<subclass name="StudentUser" discriminator-value="STUDENT">

<join table="STUDENT_USER" fetch="select">

<key column="ID" />

<property ...

... All the properties in StudentUser class

join>

subclass>

...All the other subclasses and for each one of them don’t forget the discriminator-value

After doing this you have to restart the project and let Hibernate to alter the database to reflect the changes you made, and after that you and after running your big project (Login screen project) look at the hibernate sql generated query, it will be like this:

select count (*) from USER

where USER.USER_TYPE in (‘STUDENT’, ‘EMPLOYEE’, ‘TEACHER’, ‘MERCHANT’, ‘VOUCHER’) These are the discriminator-value for each subclass

No inner join at all J

Conclusion

One of the best practices in Hibernate says that:

When you have to implement inheritance tree in Hibernate, what is the kind of the superclass? What is the behavior of this superclass and its subclasses?

If the superclass is not abstract class, and many methods for subclasses are implemented in the superclass, and the superclass tasks are more than it subclasses tasks, then you have to use <discriminator> to get the subclasses from the superclass and decrease the database load.