1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
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.
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.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
21
| Author: Brady Anderson <banderso@novell.com>
22
|***************************************************************************/
25
using System.Collections;
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.
43
public class Tag : Node
46
public Tag( string Name ) :
47
base ( Name, Guid.NewGuid().ToString(), NodeTypes.TagType )
51
public Tag( ShallowNode ShallowTagNode ) :
52
base( Store.GetStore().GetCollectionByID( ShallowTagNode.CollectionID ), ShallowTagNode )
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 )
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 )
68
changeList = new ArrayList();
69
foreach( ShallowNode sn in results )
71
Node node = new Node( collection, sn );
72
MultiValuedList mvl = node.Properties.GetProperties( "Tag" );
75
foreach( Property property in mvl )
77
Relationship relationship = property.Value as Relationship;
78
if ( relationship.NodeID == tagRelationship.NodeID )
81
changeList.Add( node );
88
if ( changeList.Count > 0 )
90
collection.Commit( changeList.ToArray( typeof( Node ) ) as Node[] );
97
#region Public Methods
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
104
public void Add( string collectionID )
106
Collection collection = Store.GetStore().GetCollectionByID( collectionID );
107
if ( collection != null )
109
lock( typeof( Simias.Tags.Tag ) )
111
// Does this tag already exist in the store
112
if ( collection.GetSingleNodeByName( this.Name ) == null )
116
collection.Commit( this );
125
throw new ExistsException( this.Name );
131
throw new NotExistException( collectionID );
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
143
/// <returns>none</returns>
144
static public void Add( string collectionID, Tag[] tags )
146
Collection collection = Store.GetStore().GetCollectionByID( collectionID );
147
if ( collection != null )
149
Simias.Tags.Tag.Add( collection, tags );
153
throw new NotExistException( collectionID );
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
163
/// <returns>none</returns>
164
static public void Add( Collection collection, Tag[] tags )
166
ArrayList addList = new ArrayList();
167
lock( typeof( Simias.Tags.Tag ) )
169
foreach( Tag tag in tags )
171
// Make sure we don't create duplicate tags
172
if ( collection.GetSingleNodeByName( tag.Name ) == null )
178
if ( addList.Count > 0 )
180
collection.Commit( addList.ToArray( typeof( Node ) ) as Node[] );
186
/// Imports an Icon which will graphically represent the tag
188
/// <param name="FileName">Source Filename</param>
191
/// <returns>true if the icon was successfully imported.</returns>
192
public bool ImportIcon( string FileName )
196
Stream srcStream = null;
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
207
if ( srcStream != null )
218
/// Imports an icon from a stream object.
220
/// <param name="SrcStream">Source Stream</param>
223
/// <returns>true if the icon was successfully imported.</returns>
224
public bool ImportIcon( Stream SrcStream )
226
bool finished = false;
227
//StoreFileNode storeFileNode = null;
230
if (this.addressBook != null)
234
// See if a photo stream already exists for this contact node.
235
// If one is found - delete it
237
this.Properties.GetSingleProperty( "Tag.Icon" );
240
Simias.Storage.Relationship relationship =
241
(Simias.Storage.Relationship) p.Value;
243
Node cPhotoNode = this.addressBook.GetNodeByID(relationship.NodeID);
244
if (cPhotoNode != null)
246
this.addressBook.Delete(cPhotoNode);
247
this.addressBook.Commit(cPhotoNode);
253
// Create the new node
257
new StoreFileNode(Common.photoProperty, srcStream);
259
Relationship parentChild = new
264
this.Properties.ModifyProperty(Common.contactToPhoto, parentChild);
265
this.addressBook.Commit(sfn);
266
this.addressBook.Commit(this);
273
BinaryReader bReader = null;
274
BinaryWriter bWriter = null;
276
// Copy the photo into the cached stream
279
// Create the new stream in the file system
280
this.photoStream = new MemoryStream();
281
bWriter = new BinaryWriter(this.photoStream);
283
// Copy the source stream
284
bReader = new BinaryReader(srcStream);
285
bReader.BaseStream.Position = 0;
286
bWriter.BaseStream.Position = 0;
288
//bWriter.Write(bReader.BaseStream, 0, bReader.BaseStream.Length);
290
// BUGBUG better algo for copying
294
i = bReader.BaseStream.ReadByte();
300
bWriter.BaseStream.WriteByte((byte) i);
303
bWriter.BaseStream.Position = 0;
314
//if (bWriter != null)
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.
331
/// <returns>none</returns>
332
static public void Remove( string collectionID, Tag[] tags )
334
Collection collection = Store.GetStore().GetCollectionByID( collectionID );
335
if ( collection != null )
337
Simias.Tags.Tag.Remove( collection, tags );
341
throw new NotExistException( collectionID );
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.
350
/// <returns>none</returns>
351
static public void Remove( Collection collection, Tag[] tags )
353
if ( tags.Length > 0 )
355
lock( typeof( Simias.Tags.Tag ) )
357
collection.Commit( collection.Delete( tags ) );
362
throw new NotExistException( "Empty array" );
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
372
public void Remove( string collectionID )
374
Collection collection = Store.GetStore().GetCollectionByID( collectionID );
375
if ( collection != null )
377
// Remove the "Tag" property from all nodes that are
378
// related to this tag
379
this.RemoveTagRelationships( collection );
381
lock( typeof( Simias.Tags.Tag ) )
383
// Does this tag already exist in the store
384
Node node = collection.GetSingleNodeByName( this.Name );
389
collection.Commit( collection.Delete( node ) );
398
throw new NotExistException( this.Name );
404
throw new NotExistException( collectionID );
411
/// Tag a node with this tag instance
412
/// An "ExistException" will be thrown if the node is already tagged
413
/// with this instance.
415
public void TagNode( Collection collection, Node node )
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" );
422
foreach( Property property in mvl )
424
Relationship relationship = property.Value as Relationship;
425
if ( relationship.NodeID == tagRelationship.NodeID )
427
throw new ExistsException( this.Name );
432
node.Properties.AddProperty( "Tag", tagRelationship );
433
collection.Commit( node );
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.
442
public void TagNode( string collectionID, Node node )
444
Collection collection = Store.GetStore().GetCollectionByID( collectionID );
445
if ( collection != null )
447
this.TagNode( collection, node );
451
throw new NotExistException( collectionID );
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
460
public void UntagNode( Collection collection, Node node )
462
// build a relationship for this tag instance
463
Relationship tagRelationship = new Relationship( collection.ID, this.ID );
465
// Get all "Tag" properties on the node
466
MultiValuedList mvl = node.Properties.GetProperties( "Tag" );
469
foreach( Property property in mvl )
471
Relationship relationship = property.Value as Relationship;
472
if ( relationship.NodeID == tagRelationship.NodeID )
474
property.DeleteProperty();
475
collection.Commit( node );
480
throw new NotExistException( this.Name );
484
throw new NotExistException( this.Name );
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
494
public void UntagNode( string collectionID, Node node )
496
Collection collection = Store.GetStore().GetCollectionByID( collectionID );
497
if ( collection != null )
499
this.UntagNode( collection, node );
503
throw new NotExistException( collectionID );