/connect-web/src/main/java/org/osforce/connect/web/tag/freemarker/EntityTag.java

http://focus-sns.googlecode.com/ · Java · 82 lines · 63 code · 10 blank · 9 comment · 6 complexity · a2445464e23a42b9f392f48e9827e44d MD5 · raw file

  1. package org.osforce.connect.web.tag.freemarker;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.osforce.connect.entity.profile.Profile;
  6. import org.osforce.connect.entity.system.Project;
  7. import org.osforce.connect.entity.system.User;
  8. import org.osforce.connect.entity.team.TeamMember;
  9. import org.osforce.connect.service.list.LinkService;
  10. import org.osforce.connect.service.team.MemberService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.util.Assert;
  13. import freemarker.core.Environment;
  14. import freemarker.ext.beans.BeanModel;
  15. import freemarker.template.TemplateDirectiveBody;
  16. import freemarker.template.TemplateDirectiveModel;
  17. import freemarker.template.TemplateException;
  18. import freemarker.template.TemplateModel;
  19. import freemarker.template.TemplateScalarModel;
  20. /**
  21. *
  22. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  23. * @since 1.1.0
  24. * @create May 30, 2011 - 10:53:01 AM
  25. * <a href="http://www.opensourceforce.org">????</a>
  26. */
  27. public class EntityTag implements TemplateDirectiveModel {
  28. private LinkService linkService;
  29. private MemberService memberService;
  30. public EntityTag() {
  31. }
  32. @Autowired
  33. public void setLinkService(LinkService linkService) {
  34. this.linkService = linkService;
  35. }
  36. @Autowired
  37. public void setMemberService(MemberService memberService) {
  38. this.memberService = memberService;
  39. }
  40. @SuppressWarnings("unchecked")
  41. public void execute(Environment env, Map params, TemplateModel[] loopVars,
  42. TemplateDirectiveBody body) throws TemplateException, IOException {
  43. BeanModel userModel = (BeanModel) params.get("user");
  44. BeanModel projectModel = (BeanModel) params.get("project");
  45. TemplateScalarModel entityModel = (TemplateScalarModel) params.get("entity");
  46. TemplateScalarModel existModel = (TemplateScalarModel) params.get("exist");
  47. Assert.notNull(entityModel, "Parameter entity can not be null!");
  48. Assert.notNull(userModel, "Parameter user can not be null!");
  49. Assert.notNull(projectModel, "Parameter project can not be null!");
  50. String entity = (String) entityModel.getAsString();
  51. User user = (User) userModel.getWrappedObject();
  52. Project project = (Project) projectModel.getWrappedObject();
  53. String exist = existModel!=null ? existModel.getAsString() : null;
  54. Object value = null;
  55. if(Profile.NAME.equals(entity)) {
  56. value = linkService.getLink(user.getProjectId(), project.getProfileId(), entity);
  57. } else if (TeamMember.NAME.equals(entity)) {
  58. value = memberService.getMember(project, user, Boolean.FALSE);
  59. }
  60. //
  61. boolean flag = false;
  62. if(StringUtils.equals("no", exist)) {
  63. flag = value==null;
  64. } else {
  65. flag = value!=null;
  66. }
  67. //
  68. if(flag) {
  69. body.render(env.getOut());
  70. }
  71. }
  72. }