class TemplateServiceImpl extends RemoteServiceServlet implements TemplateService {

	@Override
	public GUITemplate getTemplate(long templateId) throws ServerException {
		ServiceUtil.validateSession(getThreadLocalRequest());

		TemplateDAO dao = (TemplateDAO) Context.get().getBean(TemplateDAO.class);
		try {
			Template template = dao.findById(templateId);
			if (template == null)
				return null;

			dao.initialize(template);

			GUITemplate templ = new GUITemplate();
			templ.setId(templateId);
			templ.setName(template.getName());
			templ.setDescription(template.getDescription());
			templ.setReadonly(template.getReadonly() == 1);
			templ.setType(template.getType());

			AttributeSetDAO setDao = (AttributeSetDAO) Context.get().getBean(AttributeSetDAO.class);
			Map<Long, AttributeSet> sets = setDao.load(template.getTenantId());

			GUIAttribute[] attributes = new GUIAttribute[template.getAttributeNames().size()];
			int i = 0;
			for (String attrName : template.getAttributeNames()) {
				Attribute templateExtAttr = template.getAttributes().get(attrName);
				AttributeSet aSet = sets.get(templateExtAttr.getSetId());
				Attribute setExtAttr = aSet != null ? aSet.getAttribute(attrName) : null;

				GUIAttribute att = new GUIAttribute();
				att.setName(attrName);
				att.setSetId(templateExtAttr.getSetId());
				att.setSet(aSet != null ? aSet.getName() : null);
				att.setPosition(templateExtAttr.getPosition());
				att.setMandatory(templateExtAttr.getMandatory() == 1 ? true : false);
				att.setHidden(templateExtAttr.getHidden() == 1 ? true : false);
				att.setMultiple(templateExtAttr.getMultiple() == 1 ? true : false);
				att.setParent(templateExtAttr.getParent());
				att.setStringValues(templateExtAttr.getStringValues());
				att.setType(templateExtAttr.getType());
				if (StringUtils.isEmpty(templateExtAttr.getLabel()))
					att.setLabel(attrName);
				else
					att.setLabel(templateExtAttr.getLabel());
				if (templateExtAttr.getValue() instanceof String)
					att.setStringValue(templateExtAttr.getStringValue());
				else if (templateExtAttr.getValue() instanceof Long)
					att.setIntValue(templateExtAttr.getIntValue());
				else if (templateExtAttr.getValue() instanceof Double)
					att.setDoubleValue(templateExtAttr.getDoubleValue());
				else if (templateExtAttr.getValue() instanceof Date)
					att.setDateValue(ServiceUtil.convertToDate(templateExtAttr.getDateValue()));
				else if (templateExtAttr.getValue() instanceof Boolean)
					att.setBooleanValue(templateExtAttr.getBooleanValue());

				att.setEditor(templateExtAttr.getEditor());
				if (templateExtAttr.getType() == Attribute.TYPE_USER
						|| templateExtAttr.getEditor() == Attribute.EDITOR_LISTBOX) {

					String buf = setExtAttr != null ? (String) setExtAttr.getStringValue()
							: (String) templateExtAttr.getStringValue();
					List<String> list = new ArrayList<String>();
					if (buf != null) {
						if (buf.contains(",")) {
							StringTokenizer st = new StringTokenizer(buf, ",");
							while (st.hasMoreElements()) {
								String val = (String) st.nextElement();
								if (!list.contains(val))
									list.add(val);
							}
						} else
							list.add(buf.trim());
						att.setStringValue(buf);
					}
					att.setOptions(list.toArray(new String[0]));
				}
				attributes[i] = att;
				i++;
			}
			if (attributes.length > 0) {
				Arrays.sort(attributes);
				templ.setAttributes(attributes);
			}

			return templ;
		} catch (Throwable t) {
			log.error(t.getMessage(), t);
		}

		return null;
	}

}