001    /*
002    
003      $Id: SharedFile.java,v 1.12 2003/05/02 20:29:47 culdesac Exp $
004    
005    */
006    
007    package sharpster.common;
008    
009    import sharpster.common.PluginData;
010    import sharpster.common.Body;
011    
012    import java.io.Serializable;
013    import java.io.ObjectStreamException;
014    import java.util.LinkedList;
015    
016    /**
017     * A class for file management. The File objects contain a file 
018     * attribute and attributes for storing information associated with the file.
019     */
020    public class SharedFile implements Serializable {
021    
022        /**
023         * The name of the file.
024         */
025        private String fileName;
026    
027        /** 
028         * The path of the file in the CVS file tree.
029         */
030        private String pathInCVS;
031    
032        /**
033         * The version of the file in the CVS.
034         */
035        private String version;
036    
037        /**
038         * An array of users to whom the file has been shared.
039         */
040        private LinkedList sharedToUsers;
041        
042        /**
043         * The access rights with which the file has been shared.
044         */
045        private int accessRights;
046    
047        /**
048         * Plug-in information associated with the file name.
049         */
050        private PluginData pluginData;
051    
052        /** 
053         * The content of the file.
054         */
055        private Body body;
056    
057        /** 
058         * 
059         */    
060        public SharedFile() {
061            fileName = new String();
062            pathInCVS = new String();
063            version = new String();
064            sharedToUsers = new LinkedList();
065            accessRights = 0;
066        }
067    
068        /**
069         * Overloaded constructor, for deep copying of file objects.
070         */
071        public SharedFile(SharedFile origFile) {
072            fileName = new String(origFile.fileName);
073            pathInCVS = new String(origFile.pathInCVS);
074            version = new String(origFile.version);
075            accessRights = origFile.accessRights;
076            /* reference */
077            pluginData = origFile.pluginData;
078            body = origFile.body;
079            sharedToUsers = new LinkedList();
080            for (int i=0; i<origFile.sharedToUsers.size(); i++){
081                sharedToUsers.add(new String((String) 
082                                             origFile.sharedToUsers.get(i)));
083            }
084        }
085    
086        /*public Object writeReplace() throws ObjectStreamException {
087            pluginData = null;
088            removeAllPluginData
089            return this;
090        }*/
091    
092        /**
093         * Returns the name of a file.
094         */
095        public String getFileName() {
096            return fileName;
097        }
098        
099        /**
100         * Sets the name of a file.
101         */
102        public void setFileName(String name) {
103            fileName = new String(name);    
104        }
105    
106        /**
107         * Returns the path of the file in the CVS tree.
108         */
109        public String getPathInCVS() {
110            return pathInCVS;
111        }
112    
113        /**
114         * Sets the path of the file in the CVS tree.
115         */
116        public void setPathInCVS(String path) {
117            pathInCVS = new String(path);
118        }
119    
120        public String getFullPath() {
121            return new String(pathInCVS + "/" + fileName);
122        }
123    
124        /**
125         * Returns the version of the file in the CVS.
126         */
127        public String getVersion() {
128            return version;
129        }
130        
131        /**
132         * Sets the version of the file in the CVS.
133         */
134        public void setVersion(String ver) {
135            version = new String(ver);
136        }
137     
138        /**
139         * The users to whom the file is shared.
140         */
141        public String[] getSharedToUsers() {
142            String[] arr = new String[sharedToUsers.size()];
143    
144            for(int i=0;i<sharedToUsers.size();i++) {
145                arr[i] = new String((String)sharedToUsers.get(i));
146            }
147    
148            return arr;
149        }
150    
151        /**
152         *
153         */
154        public void setSharedToUsers(String[] users) {
155            sharedToUsers.clear();
156            if(users == null) return;
157            for(int i=0;i<users.length;i++) {
158                sharedToUsers.add(new String(users[i]));
159            }
160        }
161    
162        /**
163         *
164         */
165        public void addSharedToUser(String user) {
166            sharedToUsers.add(new String(user));
167        }
168    
169        /**
170         * Returns the access rights with which the files has been shared.
171         */
172        public int getAccessRights() {
173            return accessRights;
174        }
175        
176        public String getAccessRightString() {
177            String msg = new String();
178            
179            if((accessRights&4) != 0) msg += "r";   
180            if((accessRights&2) != 0) msg += "w";
181            if((accessRights&1) != 0) msg += "d";
182            
183            return msg;
184        }
185    
186        public boolean hasReadAccess(String user) {
187            boolean found = false;
188            for(int i=0;i<sharedToUsers.size();i++) {
189                String user2 = (String)sharedToUsers.get(i);
190                if(user2.equals(user)) found = true;
191            }
192            if(!found) return false;
193    
194            if((accessRights&4) == 4) return true;
195            else return false;
196        }
197    
198        public boolean hasWriteAccess(String user) {
199            boolean found = false;
200            for(int i=0;i<sharedToUsers.size();i++) {
201                String user2 = (String)sharedToUsers.get(i);
202                if(user2.equals(user)) found = true;
203            }
204            if(!found) return false;
205    
206            if((accessRights&2) == 2) return true;
207            else return false;
208        }
209    
210        public boolean hasDeleteAccess(String user) {
211            boolean found = false;
212            for(int i=0;i<sharedToUsers.size();i++) {
213                String user2 = (String)sharedToUsers.get(i);
214                if(user2.equals(user)) found = true;
215            }
216            if(!found) return false;
217    
218            if((accessRights&1) == 1) return true;
219            else return false;
220        }
221    
222        public void setAccessRights(int access) {
223            accessRights = access;
224        }
225    
226        /**
227         * Sets the access rights read for this file
228         */
229        public void setAccessRead() {
230            accessRights = accessRights | 4;
231        }
232        
233        /**
234         * Sets the access rights write for this file
235         */
236        public void setAccessWrite() {
237            accessRights = accessRights | 2;
238        }
239        
240        /**
241         * Sets the access rights delete for this file
242         */
243        public void setAccessDelete() {
244            accessRights = accessRights | 1;
245        }
246        
247        public String getPluginDataString() {
248            if(pluginData == null) return "";
249            else return pluginData.toString();      
250        }
251        
252        /**
253         * Returns the plug-in information associated with the file share 
254         * made to the specified user.
255         */
256        public PluginData getPluginData() {
257            return pluginData;
258        }
259    
260        /**
261         * Sets the plug-in information associated with the file share 
262         * made to the specified user.
263         */
264        public void setPluginData(PluginData data) {
265            pluginData = data;
266        }
267    
268        /**
269         *
270         */
271        public void setBody(Body body) {
272            this.body = body;
273        }
274        
275        /**
276         * Returns the contents of the file.
277         */
278        public Body getBody() {
279            return body;
280        }
281    
282        /**
283         *
284         */
285        public String toString() {
286            String msg = new String();
287            msg = msg + pathInCVS + " | " + fileName + " | " 
288                + version + " | " + (pluginData!=null)  
289                + " | " + (body!=null);
290            return msg;
291        }
292    }
293    
294    
295    
296    
297    
298    
299    
300    
301    
302    
303    
304    
305    
306    
307    
308    
309