KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > transaction > SmileTransaction


1 /*
2  * Copyright 2004 JavaFree.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.javabb.transaction;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.javabb.dao.entity.ISmileDAO;
24 import org.javabb.infra.Utils;
25 import org.javabb.vo.Smile;
26
27 /**
28  * @author
29  * @since 27/01/2005
30  */

31 public class SmileTransaction {
32     private ISmileDAO _smileDAO;
33
34     private static List JavaDoc _smileCache;
35
36     // ####################################################################
37
// Dependencies
38
// ####################################################################
39

40     /**
41      * @param smileDAO
42      */

43     public void setSmileDAO(ISmileDAO smileDAO) {
44         this._smileDAO = smileDAO;
45     }
46
47     // ####################################################################
48
// Métodos de negócio
49
// ####################################################################
50

51     /**
52      * @param symbol
53      * @param emotion
54      * @param filename
55      */

56     public void addSmile(String JavaDoc emotion, String JavaDoc symbol, String JavaDoc filename) {
57         this._smileDAO.create(new Smile(emotion, symbol, filename));
58     }
59
60     /**
61      * @return list
62      */

63     public List JavaDoc listAll() {
64         
65         if (_smileCache == null) {
66             _smileCache = _smileDAO.findAll();
67         }
68
69         return _smileCache;
70     }
71
72     /**
73      * @param id
74      * @return smile
75      */

76     public Smile getSmile(Long JavaDoc id) {
77         return this._smileDAO.load(id);
78     }
79
80     /**
81      * @param emoticonId
82      */

83     public void delete(Long JavaDoc emoticonId) {
84         this._smileDAO.delete(emoticonId);
85     }
86
87     /**
88      * @param emoticonId
89      * @param emotion
90      * @param symbol
91      * @param filename
92      */

93     public void updateSmile(Long JavaDoc emoticonId, String JavaDoc emotion, String JavaDoc symbol,
94             String JavaDoc filename) {
95         Smile s = this._smileDAO.load(emoticonId);
96         s.setEmoticon(emotion);
97         s.setSymbol(symbol);
98         s.setFilename(filename);
99     }
100
101     /**
102      * @param text
103      * @return text
104      */

105     public String JavaDoc replaceSmiles(String JavaDoc text) {
106
107         List JavaDoc smiles = listAll();
108
109         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(text);
110
111         try {
112
113             // Check by [code] tag
114
String JavaDoc match = "[ -code- ]";
115             String JavaDoc finalMatch = "[/ -code- ]";
116
117             List JavaDoc initCodePos = Utils.indexOf(text, match);
118             List JavaDoc finalCodePos = Utils.indexOf(text, finalMatch);
119             boolean hasCode = (initCodePos != null && !initCodePos.isEmpty()) ? true
120                     : false;
121
122             for (Iterator JavaDoc it = smiles.iterator(); it.hasNext();) {
123                 Smile smile = (Smile) it.next();
124                 String JavaDoc symbol = smile.getSymbol();
125                 if (symbol.length() == 0) {
126                     break;
127                 }
128
129                 // TODO purge hard-coded path
130
String JavaDoc imgSrc = "<img SRC='forum/images/smiles/" + smile.getFilename() + "'>";
131
132                 String JavaDoc origin = new String JavaDoc(text);
133                 int limit = 0;
134                 if (hasCode) {
135                     /*String firstPart = "";
136                     String lastPart = "";
137
138                     for (int i = 0; i < initCodePos.size(); i++) {
139                         int init = ((Integer) initCodePos.get(i)).intValue();
140                         limit = ((Integer) finalCodePos.get(i)).intValue();
141
142                         String middlePart = origin.substring(init, limit);
143
144                         firstPart = "";
145                         lastPart = "";
146                         if (i == 0) {
147                             firstPart = origin.substring(0, init);
148                             lastPart = origin.substring(limit, origin.length());
149                         } else{
150                             int oldLimit = ((Integer) finalCodePos.get(i-1)).intValue();
151                             firstPart = origin.substring(oldLimit, init);
152                             lastPart = origin.substring(limit, origin.length());
153                         }
154                         firstPart = StringUtils.replace(firstPart, symbol, imgSrc);
155                         origin = StringUtils.join(new String[] { firstPart, middlePart, lastPart });
156                     }
157                     
158                     firstPart = origin.substring(0, limit);
159                     lastPart = origin.substring(limit, origin.length());
160                     origin = StringUtils.join(new String[] { firstPart, lastPart });*/

161                     
162                 } else {
163                     origin = StringUtils.replace(origin, symbol, imgSrc);
164                 }
165                 
166                 
167                 // List smilePos = Utils.indexOf(text, symbol);
168
// for(int i=0; i<smilePos.size(); i++){
169
// int sIndex = ((Integer) smilePos.get(i)).intValue();
170
// if(hasCode){
171
// if(Utils.isBetween(sIndex, initCodePos, finalCodePos)){
172
// System.out.println("BBCODE tag is found on Smiles replace");
173
// } else{
174
// sb = sb.replace(sIndex, sIndex + symbol.length(), imgSrc);
175
// }
176
// } else{
177
// sb = sb.replace(sIndex, sIndex + symbol.length(), imgSrc);
178
// }
179
// }
180

181                 // List smilePos = Utils.indexOf(text, symbol);
182
// int index = text.lastIndexOf(symbol);
183
// if (hasCode) {
184
// while (index >= 0) {
185
// for (int i =0 ; i < initCodePos.size(); i++) {
186
// int init = ((Integer) initCodePos.get(i)).intValue();
187
// int limit = ((Integer) finalCodePos.get(i)).intValue();
188
//
189
// if ((index <= init && index >= limit) ||
190
// (index >= init && index <= limit)) {
191
// System.out.println("BBCODE tag is found on Smiles replace");
192
// System.out.println("BBCODE init: " + init + " limit: " +
193
// limit + " index: " + index);
194
// } else {
195
// //if there is no bbcode tag, make replace
196
// System.out.println("smilePos: " + smilePos +" init: " + init
197
// + " limit: " + limit + " index: " + index);
198
// sb.replace(index, index + symbol.length(), imgSrc);
199
//
200
// int nextIndex = index - symbol.length();
201
// if (nextIndex < 0) {
202
// break;
203
// }
204
// index = text.lastIndexOf(symbol, nextIndex);
205
// }
206
// }
207
//
208
//
209
// }
210
// } else {
211
//
212
// // if there is no bbcode tag, make replace
213
// while (index >= 0) {
214
// sb.replace(index, index + symbol.length(), imgSrc);
215
// int nextIndex = index - symbol.length();
216
// if (nextIndex < 0) {
217
// break;
218
// }
219
// index = text.lastIndexOf(symbol, nextIndex);
220
// }
221
// }
222

223                 // text = text.replaceAll(" \\" + symbol + " ", imgSrc);
224

225                 text = origin;
226             }
227         } catch (Exception JavaDoc e) {
228             e.printStackTrace();
229         }
230         return text;
231     }
232 }
233
Popular Tags