These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Please advise me how to implement the following behaviour
PostPosted: Wed Jul 19, 2006 9:45 am 
Newbie

Joined: Thu May 04, 2006 8:21 am
Posts: 4
Good day sirs.

I need an advice how implement the following behaviour using best NHibernate practices.

I have to implement a "transparent customization" of some entities in my system. The idea is:

* The privileged user (I have only one privileged user) creates an entity and saves it. This entity should be accessible to all other users.
* If non-privileged user gets this entity, modifies it and saves it the system have to create a clone of this entity and save it instead of updating the original entity. This newly created entity should be accessible only to this user. Next time the user gets the list of entitites the system should give him this customized entity instead of default entity. If this user deletes this entity the system should delete his customized entity - so the next time user gets the list of entities he receives the original entity created by the privileged user.
* Entities should not know the fact they are customizable or not.

Now I will describe my approach to resolving this problem.
Since each of customizable entity belongs to concrete user I have created a table for user's profiles:

CREATE TABLE [dbo].[Profiles] (
[ID] uniqueidentifier DEFAULT (newid()) NOT NULL,
[Name] nvarchar(50) COLLATE Cyrillic_General_CI_AS,
[Description] ntext COLLATE Cyrillic_General_CI_AS,
CONSTRAINT [IX_Profiles] UNIQUE ([Name]),
CONSTRAINT [PK_Profiles] PRIMARY KEY CLUSTERED ([ID])
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]


Also added two columns to each table of the customizable entities:

CREATE TABLE [dbo].[SimpleTextItems] (
[ID] uniqueidentifier DEFAULT (newid()) NOT NULL,
[Name] nvarchar(50) COLLATE Cyrillic_General_CI_AS NOT NULL,
[Description] nvarchar(255) COLLATE Cyrillic_General_CI_AS,
[ProfileID] uniqueidentifier NOT NULL,
[DefaultID] uniqueidentifier,
CONSTRAINT [PK_SimpleTextItem] PRIMARY KEY CLUSTERED ([ID]),
CONSTRAINT [FK_SimpleTextItem_Profiles] FOREIGN KEY ([ProfileID])
REFERENCES [dbo].[Profiles] ([ID])
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
ON [PRIMARY]


I store the user's profile ID into the ProfileID and the original entity's ID into DefaultID field.

All entities that require customization are successors of RequiresCustomization class (it provides an internal Profile property) so in my base DAO manager I check if the storing entity is a successor of RequiresCustomization I inspect the Profile property - in case it is null I assign a DefaultProfile (the privileged user's profile) to it, otherwise if this entity belongs to Default profile I create a clone of that entity, assign CurrentProfile (the current user's profile) and store the clone; I also evict the original entity from Session to prevent Session storing it.

This approach works good. The problems arise when customizable entity becomes a child in Parent-Child association. Here is the part of Parent's map file:

<set name="Items" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" lazy="true"
cascade="all-delete-orphan" inverse="true" persister="DataLayer.CustomizableCollectionPersister, DataLayer" order-by="[Order]">
<key column="PreferenceID"/>
<one-to-many class="PreferenceItem" />
</set>

Parent has cascade="all-delete-orphan" on the one-to-many association so if I create a parent, add a new customizable child to it and store the parent my DAO manager doesn't have a chance to assign Profile to the child. It looks like a have to realize custom persister and assign it to the customizable entities in the map file like this:

<class name='BusinessLayer.SimpleTextItem, BusinessLayer' persister="DataLayer.CustomizableEntityPersister, DataLayer" >

But I can't imagine how to evict the original entity from the session and replace it with the clone in this custom entity persister class.

I use custom persister in this Parent-Child association (CustomizableCollectionPersister : OneToManyPersister) and in CreateCollectionInitializer method I return custom colelction loader (CustomizableCollectionLoader : OuterJoinLoader, ICollectionInitializer).
It helps me to get customized Child colelction.

Maybe you know more easier ways to implement this?

Thanks a lot!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.