KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > engine > SyncItemHelper


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.server.engine;
20
21 import sync4j.framework.engine.SyncItem;
22 import sync4j.framework.engine.SyncItemKey;
23 import sync4j.framework.engine.SyncItemImpl;
24
25 import sync4j.framework.core.Item;
26 import sync4j.framework.core.ComplexData;
27 import sync4j.framework.core.Target;
28 import sync4j.framework.core.Source;
29
30 /**
31  * Helper class to convert <i>SyncItem</i>s into <i>Item</i>s and vice versa.
32  *
33  * @author Stefano Fornari @ Funambol
34  *
35  * @version $Id: SyncItemHelper.java,v 1.10 2005/03/02 20:57:39 harrie Exp $
36  *
37  */

38 public class SyncItemHelper {
39     // ---------------------------------------------------------------- Costants
40

41     public static final String JavaDoc PROPERTY_COMMAND = "PROPERTY_COMMAND";
42
43     // ---------------------------------------------------------- Public methods
44

45     /**
46      * Converts a generic <i>SyncItem</i> into an <i>Item</i> object.
47      *
48      * @param key the key the item is known by the client agent
49      * @param syncItam the <i>SyncItem</i>
50      * @param includeTarget <i>true</i> if the target must be included
51      * @param includeSource <i>true</i> if the source must be included
52      * @param includeData <i>true</i> if data must be included
53      *
54      * @return the <i>Item</i> object
55      */

56     public static Item toItem(String JavaDoc key ,
57                               SyncItem syncItem ,
58                               boolean includeTarget,
59                               boolean includeSource,
60                               boolean includeData ) {
61         ComplexData data = null;
62
63         if (includeData) {
64             data = new ComplexData(
65                        new String JavaDoc((byte[])syncItem.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT))
66                    );
67         }
68         
69         Target target = null;
70         Source source = null;
71         
72         if (includeTarget) {
73             target = new Target(key);
74         }
75         
76         if (includeSource) {
77             source = new Source(key);
78         }
79
80         return new Item(target,
81                         source,
82                         null , // meta
83
data ,
84                         false); //moreData
85
}
86     
87     /**
88      * Create a new <i>SyncItem</i> with the content of an existing one. The
89      * old SyncItem's key becomes the mappedKey of the new SyncItem.
90      *
91      * @param key
92      * @param fromSyncItem
93      */

94     public static SyncItem newMappedSyncItem(SyncItemKey key, SyncItem fromSyncItem) {
95         SyncItemImpl syncItem
96             = new SyncItemImpl(fromSyncItem.getSyncSource() ,
97                                key.getKeyAsString() ,
98                                fromSyncItem.getKey().getKeyAsString(),
99                                fromSyncItem.getState() );
100         
101         syncItem.setProperties(fromSyncItem.getProperties());
102         
103         return syncItem;
104     }
105 }
Popular Tags