1 16 17 package org.javabb.transaction; 18 19 import java.util.Iterator ; 20 import java.util.List ; 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 31 public class SmileTransaction { 32 private ISmileDAO _smileDAO; 33 34 private static List _smileCache; 35 36 40 43 public void setSmileDAO(ISmileDAO smileDAO) { 44 this._smileDAO = smileDAO; 45 } 46 47 51 56 public void addSmile(String emotion, String symbol, String filename) { 57 this._smileDAO.create(new Smile(emotion, symbol, filename)); 58 } 59 60 63 public List listAll() { 64 65 if (_smileCache == null) { 66 _smileCache = _smileDAO.findAll(); 67 } 68 69 return _smileCache; 70 } 71 72 76 public Smile getSmile(Long id) { 77 return this._smileDAO.load(id); 78 } 79 80 83 public void delete(Long emoticonId) { 84 this._smileDAO.delete(emoticonId); 85 } 86 87 93 public void updateSmile(Long emoticonId, String emotion, String symbol, 94 String filename) { 95 Smile s = this._smileDAO.load(emoticonId); 96 s.setEmoticon(emotion); 97 s.setSymbol(symbol); 98 s.setFilename(filename); 99 } 100 101 105 public String replaceSmiles(String text) { 106 107 List smiles = listAll(); 108 109 StringBuffer sb = new StringBuffer (text); 110 111 try { 112 113 String match = "[ -code- ]"; 115 String finalMatch = "[/ -code- ]"; 116 117 List initCodePos = Utils.indexOf(text, match); 118 List finalCodePos = Utils.indexOf(text, finalMatch); 119 boolean hasCode = (initCodePos != null && !initCodePos.isEmpty()) ? true 120 : false; 121 122 for (Iterator it = smiles.iterator(); it.hasNext();) { 123 Smile smile = (Smile) it.next(); 124 String symbol = smile.getSymbol(); 125 if (symbol.length() == 0) { 126 break; 127 } 128 129 String imgSrc = "<img SRC='forum/images/smiles/" + smile.getFilename() + "'>"; 131 132 String origin = new String (text); 133 int limit = 0; 134 if (hasCode) { 135 161 162 } else { 163 origin = StringUtils.replace(origin, symbol, imgSrc); 164 } 165 166 167 181 223 225 text = origin; 226 } 227 } catch (Exception e) { 228 e.printStackTrace(); 229 } 230 return text; 231 } 232 } 233 | Popular Tags |