1 package org.gnomekr.potron.service; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 import java.util.ArrayList; 6 import java.util.Date; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 import org.apache.commons.lang.NullArgumentException; 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.gnomekr.potron.data.Entry; 15 import org.gnomekr.potron.data.Header; 16 import org.gnomekr.potron.data.LanguageTeam; 17 import org.gnomekr.potron.data.Project; 18 import org.gnomekr.potron.data.Template; 19 import org.gnomekr.potron.data.TranslatedEntry; 20 import org.gnomekr.potron.data.Translation; 21 import org.gnomekr.potron.parser.IPOParserCallback; 22 import org.gnomekr.potron.parser.POParser; 23 import org.gnomekr.potron.parser.ParseException; 24 import org.gnomekr.potron.parser.ParserEntry; 25 import org.hibernate.Session; 26 import org.springframework.orm.hibernate3.SessionFactoryUtils; 27 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 28 29 /*** 30 * ProjectManager.java 31 * @author Xavier Cho 32 * @version $Revision 1.1 $ $Date: 2005/09/11 05:49:44 $ 33 */ 34 public class ProjectManager extends HibernateDaoSupport implements 35 IProjectManager { 36 37 private static Log log = LogFactory.getLog(ProjectManager.class); 38 39 /*** 40 * @see org.gnomekr.potron.service.IProjectManager#createProject(org.gnomekr.potron.data.Project) 41 */ 42 public void createProject(Project project) throws ProjectExistsException { 43 if (project == null) { 44 throw new NullArgumentException("project"); 45 } 46 47 String id = project.getId(); 48 49 if (log.isDebugEnabled()) { 50 log.debug("Creating new project : " + id); 51 } 52 53 Session session = SessionFactoryUtils.getSession( 54 getSessionFactory(), 55 false); 56 57 if (session.get(Project.class, id) != null) { 58 String msg = "The specified project name id exists : " + id; 59 throw new ProjectExistsException(msg); 60 } 61 62 project.setRegisteredDate(new Date(System.currentTimeMillis())); 63 64 session.save(project); 65 66 if (log.isInfoEnabled()) { 67 log.info("Project has been created successfully : "); 68 log.debug(project); 69 } 70 } 71 72 /*** 73 * @see org.gnomekr.potron.service.IProjectManager#getProject(java.lang.String) 74 */ 75 public Project getProject(String id) { 76 if (id == null) { 77 throw new NullArgumentException("id"); 78 } 79 80 if (log.isDebugEnabled()) { 81 log.debug("Retrieving project info : " + id); 82 } 83 84 Session session = SessionFactoryUtils.getSession( 85 getSessionFactory(), 86 false); 87 88 return (Project) session.get(Project.class, id); 89 } 90 91 /*** 92 * @see org.gnomekr.potron.service.IProjectManager#getProjects() 93 */ 94 @SuppressWarnings("unchecked") 95 public List<Project> getProjects() { 96 if (log.isDebugEnabled()) { 97 log.debug("Retrieving all projects."); 98 } 99 100 Session session = SessionFactoryUtils.getSession( 101 getSessionFactory(), 102 false); 103 104 return session.createCriteria(Project.class).setCacheable(true).list(); 105 } 106 107 /*** 108 * @see org.gnomekr.potron.service.IProjectManager#updateProject(org.gnomekr.potron.data.Project) 109 */ 110 public void updateProject(Project project) { 111 if (project == null) { 112 throw new NullArgumentException("project"); 113 } 114 115 if (log.isDebugEnabled()) { 116 log.debug("Updating project info : " + project.getName()); 117 } 118 119 Session session = SessionFactoryUtils.getSession( 120 getSessionFactory(), 121 false); 122 123 session.merge(project); 124 125 if (log.isInfoEnabled()) { 126 log.info("Project has been updated successfully : "); 127 log.debug(project); 128 } 129 } 130 131 /*** 132 * @see org.gnomekr.potron.service.IProjectManager#deleteProject(java.lang.String) 133 */ 134 public void deleteProject(String id) { 135 if (id == null) { 136 throw new NullArgumentException("id"); 137 } 138 139 if (log.isDebugEnabled()) { 140 log.debug("Deleting project info : " + id); 141 } 142 143 Session session = SessionFactoryUtils.getSession( 144 getSessionFactory(), 145 false); 146 147 Project project = getProject(id); 148 149 session.delete(project); 150 151 if (log.isInfoEnabled()) { 152 log.info("Project has been deleted successfully : "); 153 log.info(" - project : " + project); 154 } 155 } 156 157 /*** 158 * @see org.gnomekr.potron.service.IProjectManager#addLanguageTeam(org.gnomekr.potron.data.LanguageTeam) 159 */ 160 public long addLanguageTeam(LanguageTeam team) { 161 if (team == null) { 162 throw new NullArgumentException("team"); 163 } 164 if (log.isDebugEnabled()) { 165 log.debug("Adding new language team."); 166 } 167 168 Session session = SessionFactoryUtils.getSession( 169 getSessionFactory(), 170 false); 171 172 team.setRegisteredDate(new Date(System.currentTimeMillis())); 173 174 long id = (Long) session.save(team); 175 176 if (log.isInfoEnabled()) { 177 log.info("Language team has been registered successfully : "); 178 log.debug(" - team : " + team); 179 } 180 181 return id; 182 } 183 184 /*** 185 * @see org.gnomekr.potron.service.IProjectManager#getLanguageTeam(long) 186 */ 187 public LanguageTeam getLanguageTeam(long id) { 188 if (log.isDebugEnabled()) { 189 log.debug("Retrieving language team info : " + id); 190 } 191 192 Session session = SessionFactoryUtils.getSession( 193 getSessionFactory(), 194 false); 195 196 return (LanguageTeam) session.get(LanguageTeam.class, id); 197 } 198 199 /*** 200 * @see org.gnomekr.potron.service.IProjectManager#getLanguageTeams() 201 */ 202 @SuppressWarnings("unchecked") 203 public List<LanguageTeam> getLanguageTeams() { 204 if (log.isDebugEnabled()) { 205 log.debug("Retrieving all language teams."); 206 } 207 208 Session session = SessionFactoryUtils.getSession( 209 getSessionFactory(), 210 false); 211 212 return session.createCriteria(LanguageTeam.class).setCacheable(true) 213 .list(); 214 } 215 216 /*** 217 * @see org.gnomekr.potron.service.IProjectManager#updateLanguageTeam(org.gnomekr.potron.data.LanguageTeam) 218 */ 219 public void updateLanguageTeam(LanguageTeam team) { 220 if (team == null) { 221 throw new NullArgumentException("team"); 222 } 223 224 if (log.isDebugEnabled()) { 225 log.debug("Updating language team info : " + team.getId()); 226 } 227 228 Session session = SessionFactoryUtils.getSession( 229 getSessionFactory(), 230 false); 231 232 session.merge(team); 233 234 if (log.isInfoEnabled()) { 235 log.info("Language team has been updated successfully : "); 236 log.debug(team); 237 } 238 } 239 240 /*** 241 * @see org.gnomekr.potron.service.IProjectManager#removeLanguageTeam(long) 242 */ 243 public void removeLanguageTeam(long id) { 244 if (log.isDebugEnabled()) { 245 log.debug("Deleting language team : " + id); 246 } 247 248 Session session = SessionFactoryUtils.getSession( 249 getSessionFactory(), 250 false); 251 252 LanguageTeam team = getLanguageTeam(id); 253 254 session.delete(team); 255 256 if (log.isInfoEnabled()) { 257 log.info("Language team has been deleted successfully : "); 258 log.info(" - team : " + team); 259 } 260 } 261 262 /*** 263 * @throws IOException 264 * @throws ParseException 265 * @see org.gnomekr.potron.service.IProjectManager#addTemplate(java.lang.String, java.lang.String, java.lang.String, java.io.Reader) 266 */ 267 public long addTemplate( 268 String projectId, 269 String name, 270 String description, 271 Reader content) throws ParseException, IOException { 272 if (projectId == null) { 273 throw new NullArgumentException("projectId"); 274 } 275 276 if (name == null) { 277 throw new NullArgumentException("name"); 278 } 279 280 if (content == null) { 281 throw new NullArgumentException("content"); 282 } 283 284 if (log.isDebugEnabled()) { 285 log.debug("Adding new template to the project :"); 286 log.debug(" - project id : " + projectId); 287 log.debug(" - name : " + name); 288 } 289 290 final Session session = SessionFactoryUtils.getSession( 291 getSessionFactory(), 292 false); 293 294 Project project = getProject(projectId); 295 296 final Template template = new Template(); 297 template.setName(name); 298 template.setDescription(description); 299 template.setRegisteredDate(new Date(System.currentTimeMillis())); 300 template.setProject(project); 301 302 final List<Header> headers = new ArrayList<Header>(); 303 final List<Entry> entries = new ArrayList<Entry>(); 304 305 IPOParserCallback callback = new IPOParserCallback() { 306 307 public void startDocument() { 308 } 309 310 public void onComment(String comment) { 311 template.setComment(comment); 312 } 313 314 public void onHeader(String key, String value) { 315 headers.add(new Header(key, value)); 316 } 317 318 public void onHeaderPluralForm(int nplural, String expression) { 319 } 320 321 public void onEntry(ParserEntry entry) { 322 entries.add(new Entry(entry, template)); 323 } 324 325 public void endDocument() { 326 } 327 }; 328 329 POParser parser = new POParser(callback); 330 parser.parse(content); 331 332 template.setHeaders(headers); 333 template.setEntries(entries); 334 335 long id = (Long) session.save(template); 336 337 if (log.isInfoEnabled()) { 338 log.info("Project template has been registered successfully : "); 339 log.debug(" - template : " + template); 340 } 341 342 return id; 343 } 344 345 /*** 346 * @see org.gnomekr.potron.service.IProjectManager#getTemplate(long) 347 */ 348 public Template getTemplate(long id) { 349 if (log.isDebugEnabled()) { 350 log.debug("Retrieving translation template : " + id); 351 } 352 353 Session session = SessionFactoryUtils.getSession( 354 getSessionFactory(), 355 false); 356 357 return (Template) session.get(Template.class, id); 358 } 359 360 /*** 361 * @see org.gnomekr.potron.service.IProjectManager#updateTemplate(long, java.lang.String, java.lang.String, java.lang.String) 362 */ 363 public void updateTemplate( 364 long id, 365 String name, 366 String description, 367 String comment) { 368 if (name == null) { 369 throw new NullArgumentException("name"); 370 } 371 372 if (log.isDebugEnabled()) { 373 log.debug("Updating translation template : " + name); 374 } 375 376 Session session = SessionFactoryUtils.getSession( 377 getSessionFactory(), 378 false); 379 380 Template template = getTemplate(id); 381 382 template.setName(name); 383 template.setComment(comment); 384 template.setDescription(description); 385 386 session.update(template); 387 388 if (log.isInfoEnabled()) { 389 log.info("Template has been updated successfully : "); 390 log.debug(template); 391 } 392 } 393 394 /*** 395 * @see org.gnomekr.potron.service.IProjectManager#updateTemplate(long, java.lang.String, java.lang.String, java.io.Reader) 396 */ 397 public void updateTemplate( 398 long id, 399 String name, 400 String description, 401 Reader content) throws ParseException, IOException { 402 if (name == null) { 403 throw new NullArgumentException("template"); 404 } 405 406 if (content == null) { 407 throw new NullArgumentException("content"); 408 } 409 410 if (log.isDebugEnabled()) { 411 log.debug("Updating translation template : " + name); 412 } 413 414 Session session = SessionFactoryUtils.getSession( 415 getSessionFactory(), 416 false); 417 418 final Template template = getTemplate(id); 419 420 final Map<String, Entry> oldEntries = new HashMap<String, Entry>(); 421 422 for (Entry entry : template.getEntries()) { 423 if (entry != null) { 424 oldEntries.put(entry.getKey(), entry); 425 } 426 } 427 428 final List<Header> headers = new ArrayList<Header>(); 429 final List<Entry> entries = new ArrayList<Entry>(); 430 final List<Entry> newEntries = new ArrayList<Entry>(); 431 432 IPOParserCallback callback = new IPOParserCallback() { 433 434 public void startDocument() { 435 } 436 437 public void onComment(String comment) { 438 template.setComment(comment); 439 } 440 441 public void onHeader(String key, String value) { 442 headers.add(new Header(key, value)); 443 } 444 445 public void onHeaderPluralForm(int nplural, String expression) { 446 } 447 448 public void onEntry(ParserEntry entry) { 449 if (oldEntries.containsKey(entry.getMsgId())) { 450 entries.add(oldEntries.get(entry.getMsgId())); 451 } else { 452 Entry e = new Entry(entry, template); 453 entries.add(e); 454 newEntries.add(e); 455 } 456 } 457 458 public void endDocument() { 459 } 460 }; 461 462 POParser parser = new POParser(callback); 463 parser.parse(content); 464 465 template.setName(name); 466 template.setDescription(description); 467 468 template.getHeaders().clear(); 469 template.getHeaders().addAll(headers); 470 471 template.getEntries().clear(); 472 template.getEntries().addAll(entries); 473 474 session.update(template); 475 476 for (Translation translation : template.getTranslations()) { 477 for (Entry entry : newEntries) { 478 TranslatedEntry tentry = new TranslatedEntry(entry, translation); 479 translation.getEntries().add(tentry); 480 } 481 482 session.update(translation); 483 } 484 485 if (log.isInfoEnabled()) { 486 log.info("Template has been updated successfully : "); 487 log.debug(template); 488 } 489 } 490 491 /*** 492 * @see org.gnomekr.potron.service.IProjectManager#removeTemplate(long) 493 */ 494 public void removeTemplate(long id) { 495 if (log.isDebugEnabled()) { 496 log.debug("Deleting translation template : " + id); 497 } 498 499 Session session = SessionFactoryUtils.getSession( 500 getSessionFactory(), 501 false); 502 503 Template template = getTemplate(id); 504 505 session.delete(template); 506 507 if (log.isInfoEnabled()) { 508 log.info("Template has been deleted successfully : "); 509 log.info(" - template : " + template); 510 } 511 } 512 }