001/**
002 * Copyright 2011 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015
016package org.kuali.mobility.xsl.dao;
017
018import java.util.List;
019
020import javax.persistence.EntityManager;
021import javax.persistence.OptimisticLockException;
022import javax.persistence.PersistenceContext;
023import javax.persistence.Query;
024
025import org.apache.log4j.Logger;
026import org.kuali.mobility.xsl.entity.Xsl;
027import org.springframework.stereotype.Repository;
028
029@Repository
030@Deprecated
031public class XslDaoImpl implements XslDao {
032        private static final Logger LOG = Logger.getLogger(XslDaoImpl.class);
033
034    @PersistenceContext
035    private EntityManager entityManager;
036    
037    public void deleteXslById(Long id) {
038        Query query = entityManager.createQuery("delete from Xsl x where x.xslId = :id");
039        query.setParameter("id", id);
040        query.executeUpdate();
041    }
042
043    @SuppressWarnings("unchecked")
044    public List<Xsl> findAllXsl() {
045        Query query = entityManager.createQuery("select x from Xsl x order by x.id");
046        return query.getResultList();
047    }
048
049    public Xsl findXslById(Long id) {
050        Query query = entityManager.createQuery("select x from Xsl x where x.xslId = :id");
051        query.setParameter("id", id);
052        return (Xsl) query.getSingleResult();
053    }
054    
055    public Xsl findXslByCode(String code) {
056        Query query = entityManager.createQuery("select x from Xsl x where x.code = :code");
057        query.setParameter("code", code);
058        return (Xsl) query.getSingleResult();
059    }
060
061    public Long saveXsl(Xsl xsl) {
062        if (xsl == null) {
063            return null;
064        }
065            try {
066                if (xsl.getValue() != null) {
067                    xsl.setValue(xsl.getValue().trim());
068                }
069                if (xsl.getXslId() == null) {
070                    entityManager.persist(xsl);
071                } else {
072                    entityManager.merge(xsl);
073                }
074            } catch( OptimisticLockException ole ) {
075                    LOG.error(ole.getLocalizedMessage(),ole);
076                return null;
077            }
078                return xsl.getXslId();
079    }
080    
081    public EntityManager getEntityManager() {
082        return entityManager;
083    }
084
085    public void setEntityManager(EntityManager entityManager) {
086        this.entityManager = entityManager;
087    }
088    
089}