KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > collection > CollectionModule


1 /*
2  * Created on Jul 1, 2006
3  */

4 package com.openedit.archive.collection;
5
6 import java.util.Iterator JavaDoc;
7
8 import com.openedit.OpenEditException;
9 import com.openedit.WebPageRequest;
10 import com.openedit.archive.Archive;
11 import com.openedit.modules.BaseModule;
12 import com.openedit.modules.archive.ArchiveModule;
13 import com.openedit.store.Product;
14 import com.openedit.users.User;
15
16 public class CollectionModule extends BaseModule
17 {
18     protected CollectionArchive fieldCollectionArchive;
19     
20     public void addItem(WebPageRequest inReq) throws Exception JavaDoc
21     {
22         String JavaDoc productid = inReq.getRequestParameter("productid");
23         if( productid != null)
24         {
25             UserCollections cols = getUserCollections(inReq);
26             Collection col = cols.getSelectedCollection();
27             if( col == null)
28             {
29                 col = new Collection();
30                 col.setId("default");
31                 col.setName("default");
32                 cols.addCollection(col);
33                 cols.setSelectedCollection(col);
34             }
35             Archive archive = getArchive(inReq);
36             Product prod = archive.getProduct(productid);
37             
38             CollectionItem item = new CollectionItem();
39             item.setProduct(prod);
40             col.addItem(item);
41             getCollectionArchive().save(archive,cols);
42         }
43             
44     }
45     public Archive getArchive( WebPageRequest inReq ) throws OpenEditException
46     {
47         Archive archive = (Archive)inReq.getPageValue(Archive.ARCHIVE_PARAM);
48         if( archive == null)
49         {
50             ArchiveModule mod = (ArchiveModule)getModule("ArchiveModule");
51             archive = mod.getArchive(inReq);
52         }
53         return archive;
54     }
55
56     
57     public void removeItem(WebPageRequest inReq) throws Exception JavaDoc
58     {
59         String JavaDoc productid = inReq.getRequestParameter("productid");
60         if( productid != null)
61         {
62             UserCollections cols = getUserCollections(inReq);
63             cols.getSelectedCollection().removeItem(productid);
64             Archive archive = getArchive(inReq);
65             getCollectionArchive().save(archive,cols);
66         }
67     }
68
69     public void removeAll(WebPageRequest inReq) throws Exception JavaDoc
70     {
71         UserCollections cols = getUserCollections(inReq);
72         cols.getSelectedCollection().clear();
73     }
74
75     public UserCollections getUserCollections( WebPageRequest inReq) throws Exception JavaDoc
76     {
77         if( inReq.getUser() == null)
78         {
79             return null;
80         }
81         Archive archive = getArchive(inReq);
82         UserCollections col = archive.getUserCollections();
83         
84         if ( col == null)
85         {
86             col = getCollectionArchive().loadUserCollections(inReq.getUser(),archive);
87             archive.setUserCollections(col);
88         }
89         inReq.putPageValue("collection", col.getSelectedCollection());
90         inReq.putPageValue("usercollections", col);
91         return col;
92     }
93         
94     public void selectCollection(WebPageRequest inReq) throws Exception JavaDoc
95     {
96         String JavaDoc eid = inReq.getRequestParameter("collectionid");
97         if( eid != null)
98         {
99             UserCollections cols = getUserCollections(inReq);
100             Collection col = cols.getCollection(eid);
101             cols.setSelectedCollection(col);
102             //look for origURL
103
String JavaDoc origURL = inReq.getRequestParameter("origURL");
104             if( origURL != null)
105             {
106                 inReq.redirect(origURL);
107             }
108         }
109     }
110     /*
111     public void deleteCollection(WebPageRequest inReq) throws Exception
112     {
113         Translation trans = (Translation)inReq.getPageValue("translations");
114         Language selectedlan = getEditingLanguage(inReq);
115         trans.removeLanguage(selectedlan);
116         save(trans, inReq);
117         inReq.putSessionValue("editinglanguage", null);
118     }
119     */

120     
121     public void saveCollection(WebPageRequest inReq) throws Exception JavaDoc
122     {
123         UserCollections cols = getUserCollections(inReq);
124         Collection scol = cols.getSelectedCollection();
125
126         String JavaDoc id = inReq.getRequestParameter("collectionid");
127         if (id != null)
128         {
129             scol.setId(id);
130         }
131         String JavaDoc text = inReq.getRequestParameter("nametext");
132         if (text != null)
133         {
134             scol.setName(text);
135         }
136         Archive archive = getArchive(inReq);
137
138         getCollectionArchive().save( archive,cols );
139         inReq.putPageValue("message", "save complete");
140     }
141     public void addNewCollection(WebPageRequest inReq) throws Exception JavaDoc
142     {
143         String JavaDoc name = inReq.getRequestParameter("newname");
144         if( name == null)
145         {
146             return;
147         }
148         UserCollections cols = getUserCollections(inReq);
149
150         
151         String JavaDoc id = inReq.getRequestParameter("newid");
152         if( id == null)
153         {
154             id = makeId( name );
155         }
156         
157         Collection col = cols.getCollection(id);
158         if( col == null)
159         {
160             col = new Collection();
161         }
162         col.setId(id);
163         if( name == null)
164         {
165             name = id;
166         }
167         col.setName(name);
168         cols.addCollection(col);
169         cols.setSelectedCollection(col);
170         Archive archive = getArchive(inReq);
171
172         getCollectionArchive().save(archive,cols);
173     }
174
175     protected String JavaDoc makeId(String JavaDoc inName)
176     {
177         StringBuffer JavaDoc out = new StringBuffer JavaDoc(inName.length());
178         for (int i = 0; i < inName.length(); i++)
179         {
180             char c = inName.charAt(i);
181             if( Character.isLetterOrDigit(c))
182             {
183                 out.append(c);
184             }
185         }
186         String JavaDoc result = out.toString().toLowerCase();
187         return result;
188     }
189     public CollectionArchive getCollectionArchive()
190     {
191         return fieldCollectionArchive;
192     }
193
194     public void setCollectionArchive(CollectionArchive inCollectionArchive)
195     {
196         fieldCollectionArchive = inCollectionArchive;
197     }
198
199     public void deleteCollection(WebPageRequest inReq) throws Exception JavaDoc
200     {
201         String JavaDoc id = inReq.getRequestParameter("colid");
202         if( id == null)
203         {
204             return;
205         }
206         UserCollections cols = getUserCollections(inReq);
207         Collection col = cols.getCollection(id);
208         if( col != null)
209         {
210             cols.removeCollection(col);
211         }
212     }
213     
214     public void transferCollection(WebPageRequest inReq) throws Exception JavaDoc
215     {
216         UserCollections localCols = getUserCollections(inReq);
217         Collection scol = localCols.getSelectedCollection();
218         Collection foreignCol = new Collection();
219         foreignCol.setId("from-" + inReq.getUser().getUserName() + "-" + scol.getId());
220         foreignCol.setName("from-" + inReq.getUser().getUserName() + "-" + scol.getName());
221         for (Iterator JavaDoc iter = scol.getCollectionItems().iterator(); iter.hasNext();)
222         {
223             CollectionItem item = (CollectionItem) iter.next();
224             foreignCol.addItem(item);
225         }
226         
227         String JavaDoc username = inReq.getRequestParameter("username");
228         User user = getUserManager().getUser(username);
229         if (user != null)
230         {
231             Archive archive = getArchive(inReq);
232
233             UserCollections foreignCols = getCollectionArchive().loadUserCollections(user, archive);
234             foreignCols.addCollection(foreignCol);
235             getCollectionArchive().save(archive,foreignCols);
236         }
237         else
238         {
239             inReq.putPageValue("error", "The specified user was not found.");
240         }
241     }
242 }
243
Popular Tags