~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/NU-Tags/.svn/text-base/Tag.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
6
 
 | This program is free software; you can redistribute it and/or
7
 
 | modify it under the terms of version 2 of the GNU General Public License as
8
 
 | published by the Free Software Foundation.
9
 
 |
10
 
 | This program is distributed in the hope that it will be useful,
11
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 | GNU General Public License for more details.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com 
20
 
 |
21
 
 |   Author: Brady Anderson <banderso@novell.com>
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.Collections;
26
 
using System.IO;
27
 
 
28
 
using Simias;
29
 
using Simias.Client;
30
 
using Simias.Storage;
31
 
 
32
 
namespace Simias.Tags
33
 
{
34
 
        /// <summary>
35
 
        /// Class for creating a tag library in a specified collection.
36
 
        /// Nodes in the collection can then be related to tags which at
37
 
        /// a later point in time can be queried
38
 
        /// such as: give me all nodes in the collection that contain
39
 
        /// the tag "Friends".
40
 
        /// If an existing tag in the library is deleted, all node relationships
41
 
        /// to the tag are deleted as well.
42
 
        /// </summary>
43
 
        public class Tag : Node
44
 
        {
45
 
                #region Constructors
46
 
                public Tag( string Name ) :
47
 
                        base ( Name, Guid.NewGuid().ToString(), NodeTypes.TagType )
48
 
                {
49
 
                }
50
 
 
51
 
                public Tag( ShallowNode ShallowTagNode ) :
52
 
                        base( Store.GetStore().GetCollectionByID( ShallowTagNode.CollectionID ), ShallowTagNode )
53
 
                {
54
 
                }
55
 
                #endregion
56
 
 
57
 
                #region Private Methods
58
 
                // Private method to remove all relationships that exist in
59
 
                // nodes to this tag instance
60
 
                private void RemoveTagRelationships( Collection collection )
61
 
                {
62
 
                        ArrayList changeList = null;
63
 
                        Relationship tagRelationship = new Relationship( collection.ID, this.ID );
64
 
                        Property p = new Property( "Tag", tagRelationship );
65
 
                        ICSList results = collection.Search( p, SearchOp.Equal );
66
 
                        if ( results.Count > 0 )
67
 
                        {
68
 
                                changeList = new ArrayList();
69
 
                                foreach( ShallowNode sn in results )
70
 
                                {
71
 
                                        Node node = new Node( collection, sn );
72
 
                                        MultiValuedList mvl = node.Properties.GetProperties( "Tag" );
73
 
                                        if ( mvl.Count > 0 )
74
 
                                        {
75
 
                                                foreach( Property property in mvl )
76
 
                                                {
77
 
                                                        Relationship relationship = property.Value as Relationship;
78
 
                                                        if ( relationship.NodeID == tagRelationship.NodeID )
79
 
                                                        {
80
 
                                                                property.Delete();
81
 
                                                                changeList.Add( node );
82
 
                                                                break;
83
 
                                                        }
84
 
                                                }
85
 
                                        }
86
 
                                }
87
 
 
88
 
                                if ( changeList.Count > 0 )
89
 
                                {
90
 
                                        collection.Commit( changeList.ToArray( typeof( Node ) ) as Node[] );
91
 
                                }
92
 
                        }
93
 
                }
94
 
 
95
 
                #endregion
96
 
 
97
 
                #region Public Methods
98
 
                /// <summary>
99
 
                /// Add a tag to the collection
100
 
                /// An exception is thrown if the system cannot add the tag
101
 
                /// If the collectionID does not exist a "NotExistException" is thrown
102
 
                /// If the tag already exists an "ExistsException" is thrown
103
 
                /// </summary>
104
 
                public void Add( string collectionID )
105
 
                {
106
 
                        Collection collection = Store.GetStore().GetCollectionByID( collectionID );
107
 
                        if ( collection != null )
108
 
                        {
109
 
                                lock( typeof( Simias.Tags.Tag ) )
110
 
                                {
111
 
                                        // Does this tag already exist in the store
112
 
                                        if ( collection.GetSingleNodeByName( this.Name ) == null )
113
 
                                        {
114
 
                                                try
115
 
                                                {
116
 
                                                        collection.Commit( this );
117
 
                                                }
118
 
                                                catch( Exception e )
119
 
                                                {
120
 
                                                        throw e;
121
 
                                                }
122
 
                                        }
123
 
                                        else
124
 
                                        {
125
 
                                                throw new ExistsException( this.Name );
126
 
                                        }
127
 
                                }
128
 
                        }
129
 
                        else
130
 
                        {
131
 
                                throw new NotExistException( collectionID );
132
 
                        }
133
 
 
134
 
                        return;
135
 
                }
136
 
 
137
 
                /// <summary>
138
 
                /// Bulk method to add tags to a collection.
139
 
                /// An exception is raised if the method fails to
140
 
                /// add all the tags in the array.
141
 
                /// Note: the tag library is locked while the tags are added
142
 
                /// </summary>
143
 
                /// <returns>none</returns>
144
 
                static public void Add( string collectionID, Tag[] tags )
145
 
                {
146
 
                        Collection collection = Store.GetStore().GetCollectionByID( collectionID );
147
 
                        if ( collection != null )
148
 
                        {
149
 
                                Simias.Tags.Tag.Add( collection, tags );
150
 
                        }
151
 
                        else
152
 
                        {
153
 
                                throw new NotExistException( collectionID );
154
 
                        }
155
 
                }
156
 
 
157
 
                /// <summary>
158
 
                /// Bulk method to add tags to a collection.
159
 
                /// An exception is raised if the method fails to
160
 
                /// add all the tags in the array.
161
 
                /// Note: the tag library is locked while the tags are added
162
 
                /// </summary>
163
 
                /// <returns>none</returns>
164
 
                static public void Add( Collection collection, Tag[] tags )
165
 
                {
166
 
                        ArrayList addList = new ArrayList();
167
 
                        lock( typeof( Simias.Tags.Tag ) )
168
 
                        {
169
 
                                foreach( Tag tag in tags )
170
 
                                {
171
 
                                        // Make sure we don't create duplicate tags
172
 
                                        if ( collection.GetSingleNodeByName( tag.Name ) == null )
173
 
                                        {
174
 
                                                addList.Add( tag );
175
 
                                        }
176
 
                                }
177
 
 
178
 
                                if ( addList.Count > 0 )
179
 
                                {
180
 
                                        collection.Commit( addList.ToArray( typeof( Node ) ) as Node[] );
181
 
                                }
182
 
                        }
183
 
                }
184
 
 
185
 
                /// <summary>
186
 
                /// Imports an Icon which will graphically represent the tag
187
 
                /// </summary>
188
 
                /// <param name="FileName">Source Filename</param>
189
 
                /// <remarks>
190
 
                /// </remarks>
191
 
                /// <returns>true if the icon was successfully imported.</returns>
192
 
                public bool ImportIcon( string FileName )
193
 
                {
194
 
                        bool    result = false;
195
 
                        /*
196
 
                        Stream  srcStream = null;
197
 
 
198
 
                        try
199
 
                        {
200
 
                                srcStream = new FileStream( FileName, FileMode.Open );
201
 
                                result = this.ImportIcon( srcStream );
202
 
                                // BUGBUG store the source file name in the store - where it came from
203
 
                        }
204
 
                        catch{}
205
 
                        finally
206
 
                        {
207
 
                                if ( srcStream != null )
208
 
                                {
209
 
                                        srcStream.Close();
210
 
                                }
211
 
                        }
212
 
                        */
213
 
 
214
 
                        return( result );
215
 
                }
216
 
 
217
 
                /// <summary>
218
 
                /// Imports an icon from a stream object.
219
 
                /// </summary>
220
 
                /// <param name="SrcStream">Source Stream</param>
221
 
                /// <remarks>
222
 
                /// </remarks>
223
 
                /// <returns>true if the icon was successfully imported.</returns>
224
 
                public bool     ImportIcon( Stream SrcStream )
225
 
                {
226
 
                        bool                    finished = false;
227
 
                        //StoreFileNode storeFileNode = null;
228
 
 
229
 
                        /*
230
 
                        if (this.addressBook != null)
231
 
                        {
232
 
                                try
233
 
                                {
234
 
                                        // See if a photo stream already exists for this contact node.
235
 
                                        // If one is found - delete it
236
 
                                        Property p =
237
 
                                                this.Properties.GetSingleProperty( "Tag.Icon" );
238
 
                                        if ( p != null )
239
 
                                        {
240
 
                                                Simias.Storage.Relationship relationship =
241
 
                                                        (Simias.Storage.Relationship) p.Value;
242
 
 
243
 
                                                Node cPhotoNode = this.addressBook.GetNodeByID(relationship.NodeID);
244
 
                                                if (cPhotoNode != null)
245
 
                                                {
246
 
                                                        this.addressBook.Delete(cPhotoNode);
247
 
                                                        this.addressBook.Commit(cPhotoNode);
248
 
                                                }
249
 
                                        }
250
 
                                }
251
 
                                catch{}
252
 
 
253
 
                                // Create the new node
254
 
                                try
255
 
                                {
256
 
                                        sfn =
257
 
                                                new StoreFileNode(Common.photoProperty, srcStream);
258
 
 
259
 
                                        Relationship parentChild = new
260
 
                                                Relationship(
261
 
                                                this.addressBook.ID,
262
 
                                                sfn.ID);
263
 
 
264
 
                                        this.Properties.ModifyProperty(Common.contactToPhoto, parentChild);
265
 
                                        this.addressBook.Commit(sfn);
266
 
                                        this.addressBook.Commit(this);
267
 
                                        finished = true;
268
 
                                }
269
 
                                catch{}
270
 
                        }
271
 
                        else
272
 
                        {
273
 
                                BinaryReader    bReader = null;
274
 
                                BinaryWriter    bWriter = null;
275
 
 
276
 
                                // Copy the photo into the cached stream
277
 
                                try
278
 
                                {
279
 
                                        // Create the new stream in the file system
280
 
                                        this.photoStream = new MemoryStream();
281
 
                                        bWriter = new BinaryWriter(this.photoStream);
282
 
 
283
 
                                        // Copy the source stream
284
 
                                        bReader = new BinaryReader(srcStream);
285
 
                                        bReader.BaseStream.Position = 0;
286
 
                                        bWriter.BaseStream.Position = 0;
287
 
 
288
 
                                        //bWriter.Write(bReader.BaseStream, 0, bReader.BaseStream.Length);
289
 
                                        
290
 
                                        // BUGBUG better algo for copying
291
 
                                        int i = 0;
292
 
                                        while(true)
293
 
                                        {
294
 
                                                i = bReader.BaseStream.ReadByte();
295
 
                                                if(i == -1)
296
 
                                                {
297
 
                                                        break;
298
 
                                                }
299
 
 
300
 
                                                bWriter.BaseStream.WriteByte((byte) i);
301
 
                                        }
302
 
 
303
 
                                        bWriter.BaseStream.Position = 0;
304
 
                                        finished = true;
305
 
                                }
306
 
                                catch{}
307
 
                                finally
308
 
                                {
309
 
                                        if (bReader != null)
310
 
                                        {
311
 
                                                bReader.Close();
312
 
                                        }
313
 
 
314
 
                                        //if (bWriter != null)
315
 
                                        //{
316
 
                                        //      bWriter.Close();
317
 
                                        //}
318
 
                                }
319
 
                        }
320
 
                        */
321
 
 
322
 
                        return finished;
323
 
                }
324
 
 
325
 
                /// <summary>
326
 
                /// Bulk method to remove tags from a collection
327
 
                /// where the caller passes in a Collection ID.
328
 
                /// An exception is raised if the method fails to
329
 
                /// remove all tags in the array.
330
 
                /// </summary>
331
 
                /// <returns>none</returns>
332
 
                static public void Remove( string collectionID, Tag[] tags )
333
 
                {
334
 
                        Collection collection = Store.GetStore().GetCollectionByID( collectionID );
335
 
                        if ( collection != null )
336
 
                        {
337
 
                                Simias.Tags.Tag.Remove( collection, tags );
338
 
                        }
339
 
                        else
340
 
                        {
341
 
                                throw new NotExistException( collectionID );
342
 
                        }
343
 
                }
344
 
 
345
 
                /// <summary>
346
 
                /// Bulk method to remove tags from a collection
347
 
                /// An exception is raised if the method fails to
348
 
                /// remove all tags in the array.
349
 
                /// </summary>
350
 
                /// <returns>none</returns>
351
 
                static public void Remove( Collection collection, Tag[] tags )
352
 
                {
353
 
                        if ( tags.Length > 0 )
354
 
                        {
355
 
                                lock( typeof( Simias.Tags.Tag ) )
356
 
                                {
357
 
                                        collection.Commit( collection.Delete( tags ) );
358
 
                                }
359
 
                        }
360
 
                        else
361
 
                        {
362
 
                                throw new NotExistException( "Empty array" );
363
 
                        }
364
 
                }
365
 
 
366
 
                /// <summary>
367
 
                /// Remove an existing tag from the collection
368
 
                /// All nodes that have this tag related will have the
369
 
                /// relationship broken before the tag is deleted.
370
 
                /// A "NotExistException" is thrown if the collection or tag does not exist
371
 
                /// </summary>
372
 
                public void Remove( string collectionID )
373
 
                {
374
 
                        Collection collection = Store.GetStore().GetCollectionByID( collectionID );
375
 
                        if ( collection != null )
376
 
                        {
377
 
                                // Remove the "Tag" property from all nodes that are
378
 
                                // related to this tag
379
 
                                this.RemoveTagRelationships( collection );
380
 
 
381
 
                                lock( typeof( Simias.Tags.Tag ) )
382
 
                                {
383
 
                                        // Does this tag already exist in the store
384
 
                                        Node node = collection.GetSingleNodeByName( this.Name );
385
 
                                        if ( node != null )
386
 
                                        {
387
 
                                                try
388
 
                                                {
389
 
                                                        collection.Commit( collection.Delete( node ) );
390
 
                                                }
391
 
                                                catch( Exception e )
392
 
                                                {
393
 
                                                        throw e;
394
 
                                                }
395
 
                                        }
396
 
                                        else
397
 
                                        {
398
 
                                                throw new NotExistException( this.Name );
399
 
                                        }
400
 
                                }
401
 
                        }
402
 
                        else
403
 
                        {
404
 
                                throw new NotExistException( collectionID );
405
 
                        }
406
 
 
407
 
                        return;
408
 
                }
409
 
 
410
 
                /// <summary>
411
 
                /// Tag a node with this tag instance
412
 
                /// An "ExistException" will be thrown if the node is already tagged
413
 
                /// with this instance.
414
 
                /// </summary>
415
 
                public void TagNode( Collection collection, Node node )
416
 
                {
417
 
                        // Add a relationship that will reference the tag.
418
 
                        Relationship tagRelationship = new Relationship( collection.ID, this.ID );
419
 
                        MultiValuedList mvl = node.Properties.GetProperties( "Tag" );
420
 
                        if ( mvl.Count > 0 )
421
 
                        {
422
 
                                foreach( Property property in mvl )
423
 
                                {
424
 
                                        Relationship relationship = property.Value as Relationship;
425
 
                                        if ( relationship.NodeID == tagRelationship.NodeID )
426
 
                                        {
427
 
                                                throw new ExistsException( this.Name );
428
 
                                        }
429
 
                                }
430
 
                        }
431
 
 
432
 
                        node.Properties.AddProperty( "Tag", tagRelationship );
433
 
                        collection.Commit( node );
434
 
                }
435
 
 
436
 
                /// <summary>
437
 
                /// Tag a node with this tag instance
438
 
                /// A "NotExistException" will be thrown if the collectionID is invalid
439
 
                /// An "ExistException" will be thrown if the node is already tagged
440
 
                /// with this instance.
441
 
                /// </summary>
442
 
                public void TagNode( string collectionID, Node node )
443
 
                {
444
 
                        Collection collection = Store.GetStore().GetCollectionByID( collectionID );
445
 
                        if ( collection != null )
446
 
                        {
447
 
                                this.TagNode( collection, node );
448
 
                        }
449
 
                        else
450
 
                        {
451
 
                                throw new NotExistException( collectionID );
452
 
                        }
453
 
                }
454
 
 
455
 
                /// <summary>
456
 
                /// Untag a previously tagged node
457
 
                /// A "NotExistException" will be thrown if any of the following occurs:
458
 
                ///   this (tag) has not been previously tagged to the node
459
 
                /// </summary>
460
 
                public void UntagNode( Collection collection, Node node )
461
 
                {
462
 
                        // build a relationship for this tag instance
463
 
                        Relationship tagRelationship = new Relationship( collection.ID, this.ID );
464
 
 
465
 
                        // Get all "Tag" properties on the node
466
 
                        MultiValuedList mvl = node.Properties.GetProperties( "Tag" );
467
 
                        if ( mvl.Count > 0 )
468
 
                        {
469
 
                                foreach( Property property in mvl )
470
 
                                {
471
 
                                        Relationship relationship = property.Value as Relationship;
472
 
                                        if ( relationship.NodeID == tagRelationship.NodeID )
473
 
                                        {
474
 
                                                property.DeleteProperty();
475
 
                                                collection.Commit( node );
476
 
                                                return;
477
 
                                        }
478
 
                                }
479
 
 
480
 
                                throw new NotExistException( this.Name );
481
 
                        }
482
 
                        else
483
 
                        {
484
 
                                throw new NotExistException( this.Name );
485
 
                        }
486
 
                }
487
 
 
488
 
                /// <summary>
489
 
                /// Untag a previously tagged node
490
 
                /// A "NotExistException" will be thrown if any of the following occurs:
491
 
                ///   collectionID does not exist
492
 
                ///   this (tag) has not been previously tagged to the node
493
 
                /// </summary>
494
 
                public void UntagNode( string collectionID, Node node )
495
 
                {
496
 
                        Collection collection = Store.GetStore().GetCollectionByID( collectionID );
497
 
                        if ( collection != null )
498
 
                        {
499
 
                                this.UntagNode( collection, node );
500
 
                        }
501
 
                        else
502
 
                        {
503
 
                                throw new NotExistException( collectionID );
504
 
                        }
505
 
                }
506
 
                #endregion
507
 
        }
508
 
}