class HibernateAttributeSetDAO extends HibernatePersistentObjectDAO<AttributeSet> implements AttributeSetDAO {

	@Override
	public boolean store(AttributeSet set) throws PersistenceException {
		if (!checkStoringAspect())
			return false;

		// Enforce the set specifications in all the attributes
		for (Attribute att : set.getAttributes().values()) {
			att.setSetId(set.getId());
		}
		boolean result = super.store(set);
		for (Attribute att : set.getAttributes().values())
			att.setSetId(set.getId());
		super.store(set);
		optionsDao.deleteOrphaned(set.getId(), set.getAttributeNames());

		/*
		 * Update the attributes referenced in the templates
		 */
		List<Template> templates = templateDao.findAll(set.getTenantId());
		for (Template template : templates) {
			templateDao.initialize(template);
			List<String> names = template.getAttributeNames(set.getId());
			for (String name : names) {
				Attribute setAttribute = set.getAttribute(name);
				if (setAttribute != null) {
					// the attribute exists both in template and set so update
					// it but preserve the position declared in the template

					Attribute templateAttribute = template.getAttribute(name);
					int actualPosition = templateAttribute.getPosition();

					try {
						Attribute clonedAttribute = (Attribute) setAttribute.clone();
						clonedAttribute.setPosition(actualPosition);
						template.getAttributes().put(name, clonedAttribute);
					} catch (CloneNotSupportedException e) {

					}
				} else {
					// the attribute exists in template but not in the set so
					// remove it
					template.removeAttribute(name);
				}
			}
			templateDao.store(template);
		}

		return result;
	}

	public  HibernateAttributeSetDAO();

}