001    /**
002     * Copyright (C) Intersect 2012.
003     *
004     * This module contains Proprietary Information of Intersect,
005     * and should be treated as Confidential.
006     */
007    package au.org.intersect.exsite9.service;
008    
009    import java.util.List;
010    
011    import javax.persistence.EntityManager;
012    import javax.persistence.EntityManagerFactory;
013    
014    import au.org.intersect.exsite9.dao.MetadataAttributeDAO;
015    import au.org.intersect.exsite9.dao.MetadataCategoryDAO;
016    import au.org.intersect.exsite9.dao.factory.MetadataAttributeDAOFactory;
017    import au.org.intersect.exsite9.dao.factory.MetadataCategoryDAOFactory;
018    import au.org.intersect.exsite9.domain.MetadataAttribute;
019    import au.org.intersect.exsite9.domain.MetadataCategory;
020    import au.org.intersect.exsite9.domain.MetadataCategoryType;
021    import au.org.intersect.exsite9.domain.MetadataCategoryUse;
022    import au.org.intersect.exsite9.domain.MetadataCategoryViewConfiguration;
023    import au.org.intersect.exsite9.domain.MetadataValue;
024    
025    /**
026     * A service for manipulating {@link MetadataCategory}s
027     */
028    public final class MetadataCategoryService implements IMetadataCategoryService
029    {
030        private final EntityManagerFactory emf;
031        private final MetadataCategoryDAOFactory metadataCategoryDAOFactory;
032        private final MetadataAttributeDAOFactory metadataAttributeDAOFactory;
033    
034        public MetadataCategoryService(final EntityManagerFactory emf, final MetadataCategoryDAOFactory metadataCategoryDAOFactory, final MetadataAttributeDAOFactory metadataAttributeDAOFactory)
035        {
036            this.emf = emf;
037            this.metadataCategoryDAOFactory = metadataCategoryDAOFactory;
038            this.metadataAttributeDAOFactory = metadataAttributeDAOFactory;
039        }
040    
041        /** 
042         * @{inheritDoc}
043         */
044        @Override
045        public MetadataCategory createNewMetadataCategory(final String name, final String description, final MetadataCategoryType type, final MetadataCategoryUse use,
046                final boolean inextensible, final boolean imported, final List<MetadataValue> values, final MetadataAttribute metadataAttribute)
047        {
048            final EntityManager em = this.emf.createEntityManager();
049            try
050            {
051                final MetadataCategoryDAO mdcDAO = this.metadataCategoryDAOFactory.createInstance(em);
052                final MetadataAttributeDAO mdaDAO = this.metadataAttributeDAOFactory.createInstance(em);
053    
054                final MetadataCategory mdc = new MetadataCategory(name, type, use);
055                mdc.setDescription(description);
056                mdc.setValues(values);
057                mdc.setInextensible(inextensible);
058                mdc.setImported(imported);
059                
060                if (metadataAttribute != null)
061                {
062                    mdaDAO.createMetadataAttribute(metadataAttribute);
063                    mdc.setMetadataAttribute(metadataAttribute);
064                }
065    
066                mdcDAO.createMetadataCategory(mdc);
067                return mdc;
068            }
069            finally
070            {
071                em.close();
072            }
073        }
074    
075        /**
076         * @{inheritDoc}
077         */
078        @Override
079        public void deleteMetadataCategory(final MetadataCategory metadataCategory)
080        {
081            final EntityManager em = this.emf.createEntityManager();
082            try
083            {
084                final MetadataCategoryDAO mdcDAO = this.metadataCategoryDAOFactory.createInstance(em);
085    
086                final MetadataAttribute mda = metadataCategory.getMetadataAttribute();
087                if (mda != null)
088                {
089                    final MetadataAttributeDAO mdaDAO = this.metadataAttributeDAOFactory.createInstance(em);
090                    metadataCategory.setMetadataAttribute(null);
091                    mdcDAO.updateMetadataCategory(metadataCategory);
092                    mdaDAO.delete(mda);
093                }
094    
095                final MetadataCategoryViewConfiguration mcvc = MetadataCategoryViewConfigService.getMetadataCategoryViewConfiguration(em, metadataCategory);
096                if (mcvc != null)
097                {
098                    em.remove(mcvc);
099                }
100    
101                mdcDAO.delete(metadataCategory);
102            }
103            finally
104            {
105                em.close();
106            }
107        }
108    
109        @Override
110        public MetadataCategory findById(final Long id)
111        {
112            final EntityManager em = this.emf.createEntityManager();
113            try
114            {
115                final MetadataCategoryDAO mdcDAO = this.metadataCategoryDAOFactory.createInstance(em);
116                return mdcDAO.findById(id);
117            }
118            finally
119            {
120                em.close();
121            }
122        }
123    
124        @Override
125        public void updateMetadataCategory(final MetadataCategory existingMetadataCategoryToUpdate, final String name, final String description,
126                final MetadataCategoryUse use, final boolean inExtensible, final List<MetadataValue> values, final MetadataAttribute newMetadataAttribute)
127        {
128            final EntityManager em = this.emf.createEntityManager();
129            try
130            {
131                final MetadataCategoryDAO mdcDAO = this.metadataCategoryDAOFactory.createInstance(em);
132                final MetadataAttributeDAO mdaDAO = this.metadataAttributeDAOFactory.createInstance(em);
133    
134                existingMetadataCategoryToUpdate.setName(name);
135                existingMetadataCategoryToUpdate.setDescription(description);
136                existingMetadataCategoryToUpdate.setUse(use);
137                existingMetadataCategoryToUpdate.setInextensible(inExtensible);
138                existingMetadataCategoryToUpdate.setValues(values);
139    
140                final MetadataAttribute oldMetadataAttribute = existingMetadataCategoryToUpdate.getMetadataAttribute();
141                existingMetadataCategoryToUpdate.setMetadataAttribute(newMetadataAttribute);
142                mdcDAO.updateMetadataCategory(existingMetadataCategoryToUpdate);
143    
144                if (oldMetadataAttribute != null)
145                {
146                    if (newMetadataAttribute != null)
147                    {
148                        mdaDAO.updateMetadataAttribute(newMetadataAttribute);
149                    }
150                    else
151                    {
152                        mdaDAO.delete(oldMetadataAttribute);
153                    }
154                }
155            }
156            finally
157            {
158                em.close();
159            }
160        }
161    
162        @Override
163        public MetadataValue addValueToMetadataCategory(final MetadataCategory metadataCategory, final String metadataValue)
164        {
165            if (metadataCategory.getType() == MetadataCategoryType.CONTROLLED_VOCABULARY)
166            {
167                for (final MetadataValue existingValue : metadataCategory.getValues())
168                {
169                    if (existingValue.getValue().equals(metadataValue))
170                    {
171                        return existingValue;
172                    }
173                }
174            }
175    
176            final EntityManager em = this.emf.createEntityManager();
177    
178            try
179            {
180                final MetadataValue newValue = new MetadataValue(metadataValue);
181    
182                em.persist(newValue);
183                metadataCategory.getValues().add(newValue);
184                final MetadataCategoryDAO mdcDAO = this.metadataCategoryDAOFactory.createInstance(em);
185                mdcDAO.updateMetadataCategory(metadataCategory);
186    
187                return newValue;
188            }
189            finally
190            {
191                em.close();
192            }
193        }
194    }