Package com.dotmarketing.business

Source Code of com.dotmarketing.business.VersionableFactoryImpl

package com.dotmarketing.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dotcms.repackage.org.apache.commons.beanutils.BeanUtils;

import com.dotmarketing.beans.Identifier;
import com.dotmarketing.beans.Inode;
import com.dotmarketing.beans.VersionInfo;
import com.dotmarketing.db.DbConnectionFactory;
import com.dotmarketing.db.HibernateUtil;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;

/**
*
* @author will
*
*/
public class VersionableFactoryImpl extends VersionableFactory {

  IdentifierAPI iapi = null;
  IdentifierCache icache = null;

  public VersionableFactoryImpl() {
    iapi = APILocator.getIdentifierAPI();
    icache = CacheLocator.getIdentifierCache();
  }

  @Override
  protected Versionable findWorkingVersion(String id) throws DotDataException, DotStateException {

    Identifier identifier = iapi.find(id);
    if(identifier==null || !InodeUtils.isSet(identifier.getInode())){
      throw new DotDataException("identifier:" + id +" not found");
    }
    if(identifier.getAssetType().equals("contentlet"))
        throw new DotDataException("Contentlets could have working versions for each language");

    VersionInfo vinfo = getVersionInfo(identifier.getId());

    Class clazz = InodeUtils.getClassByDBType(identifier.getAssetType());
    HibernateUtil dh = new HibernateUtil(clazz);
    dh.setQuery("from inode in class " + clazz.getName() + " where inode.inode=?");
    dh.setParam(vinfo.getWorkingInode());
    Logger.debug(this.getClass(), "findWorkingVersion query: " + dh.getQuery());

    Versionable v =(Versionable) dh.load();
    if(v.getVersionId() ==null){
      throw new DotStateException("Invalid working version for identifier : " +id + " / working inode : " + vinfo.getWorkingInode());
    }
    return v;


  }

  @Override
  protected Versionable findLiveVersion(String id) throws DotDataException, DotStateException {
    Identifier identifier = iapi.find(id);
    if(identifier==null || !InodeUtils.isSet(identifier.getInode())){
      throw new DotDataException("identifier:" + id +" not found");
    }
    if(identifier.getAssetType().equals("contentlet"))
            throw new DotDataException("Contentlets could have live versions for each language");

    VersionInfo vinfo = getVersionInfo(identifier.getId());

    Class clazz = InodeUtils.getClassByDBType(identifier.getAssetType());
    if(UtilMethods.isSet(vinfo)) {
      if(UtilMethods.isSet(vinfo.getLiveInode())){
          HibernateUtil dh = new HibernateUtil(clazz);
          dh.setQuery("from inode in class " + clazz.getName() + " where inode.inode=?");
          dh.setParam(vinfo.getLiveInode());
          Logger.debug(this.getClass(), "findLiveVersion query: " + dh.getQuery());
          return (Versionable) dh.load();
      }else{
        return null;
      }
    }else {
        // hey! there is no live version for this versionable
        return null;
    }
  }
  @Override
  protected Versionable findDeletedVersion(String id) throws DotDataException, DotStateException {
    Identifier identifier = iapi.find(id);
    if(identifier ==null){
      throw new DotDataException("identifier:" + id +" not found");
    }
    Class clazz = InodeUtils.getClassByDBType(identifier.getAssetType());
    HibernateUtil dh = new HibernateUtil(clazz);
    dh.setQuery("from inode in class " + clazz.getName() + " where identifier = ? and inode.type='" + identifier.getAssetType() + "' and deleted="
        + DbConnectionFactory.getDBTrue());
    dh.setParam(id);
    Logger.debug(this.getClass(), "findDeletedVersion query: " + dh.getQuery());
    return (Versionable) dh.load();
  }
  @Override
  protected List<Versionable> findAllVersions(String id) throws DotDataException, DotStateException {
    Identifier identifier = iapi.find(id);
    if(identifier ==null){
      throw new DotDataException("identifier:" + id +" not found");
    }
    Class clazz = InodeUtils.getClassByDBType(identifier.getAssetType());
    if(clazz.equals(Inode.class))
        return new ArrayList<Versionable>(1);
    HibernateUtil dh = new HibernateUtil(clazz);
    dh.setQuery("from inode in class " + clazz.getName() + " where inode.identifier = ? and inode.type='" + identifier.getAssetType() + "' order by mod_date desc");
    dh.setParam(id);
    Logger.debug(this.getClass(), "findAllVersions query: " + dh.getQuery());
    return (List<Versionable>) dh.list();
  }

    @Override
    protected VersionInfo getVersionInfo(String identifier) throws DotDataException,
            DotStateException {
        VersionInfo vi = icache.getVersionInfo(identifier);
        if(vi==null || vi.getWorkingInode().equals("NOTFOUND")) {
            Identifier ident = APILocator.getIdentifierAPI().find(identifier);
            if(ident==null || !UtilMethods.isSet(ident.getId()))
                return null;
            Class clazz = UtilMethods.getVersionInfoType(ident.getAssetType());
            HibernateUtil dh = new HibernateUtil(clazz);
            dh.setQuery("from "+clazz.getName()+" where identifier=?");
            dh.setParam(identifier);
            Logger.debug(this.getClass(), "getVersionInfo query: "+dh.getQuery());
            vi=(VersionInfo)dh.load();
            if(!UtilMethods.isSet(vi.getIdentifier())) {
              vi.setIdentifier(identifier);
              vi.setWorkingInode("NOTFOUND");
            }
            icache.addVersionInfoToCache(vi);
        }
        if(vi.getWorkingInode().equals("NOTFOUND"))
            return null;
        else
          return vi;
    }

  /**
   * reload versionInfo from db (JIRA-7203)
   * @param info
   * @return
   * @throws DotDataException
   * @throws DotStateException
   */
    @Override
    protected VersionInfo findVersionInfoFromDb(Identifier identifer) throws DotDataException,
            DotStateException {
            Class clazz = UtilMethods.getVersionInfoType(identifer.getAssetType());
            VersionInfo vi= null;
            if(clazz != null) {
              HibernateUtil dh = new HibernateUtil(clazz);
              dh.setQuery("from "+clazz.getName()+" where identifier=?");
              dh.setParam(identifer.getId());
              Logger.debug(this.getClass(), "getVersionInfo query: "+dh.getQuery());
              vi=(VersionInfo)dh.load();
            }
            if(vi ==null || !UtilMethods.isSet(vi.getIdentifier())) {
              try {
                    vi = (VersionInfo) clazz.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            return vi;

    }



    @Override
    protected void saveVersionInfo(VersionInfo info, boolean updateVersionTS) throws DotDataException, DotStateException {

      //reload versionInfo from db (JIRA-7203)
        Identifier ident = APILocator.getIdentifierAPI().find(info.getIdentifier());
        VersionInfo vi=(VersionInfo) findVersionInfoFromDb(ident);
        boolean isNew = vi==null || !InodeUtils.isSet(vi.getIdentifier());
        try {
      BeanUtils.copyProperties(vi, info);
    } catch (Exception e) {
      throw new DotDataException(e.getMessage());
    }

        if(updateVersionTS) {
          vi.setVersionTs(new Date());
        }

        if(isNew) {
            HibernateUtil.save(vi);
        }
        else {
            HibernateUtil.saveOrUpdate(vi);
        }
        HibernateUtil.flush();
        icache.removeVersionInfoFromCache(vi.getIdentifier());

    }

    @Override
    protected ContentletVersionInfo getContentletVersionInfo(String identifier, long lang) throws DotDataException, DotStateException {
        ContentletVersionInfo contv = icache.getContentVersionInfo(identifier, lang);
        if(contv==null) {
            HibernateUtil dh = new HibernateUtil(ContentletVersionInfo.class);
            dh.setQuery("from "+ContentletVersionInfo.class.getName()+" where identifier=? and lang=?");
            dh.setParam(identifier);
            dh.setParam(lang);
            Logger.debug(this.getClass(), "getContentletVersionInfo query: "+dh.getQuery());
            contv = (ContentletVersionInfo)dh.load();
            if(UtilMethods.isSet(contv.getIdentifier()))
                icache.addContentletVersionInfoToCache(contv);
        }
        return contv;
    }

    @Override
    protected ContentletVersionInfo findContentletVersionInfoInDB(String identifier, long lang)throws DotDataException, DotStateException {
      ContentletVersionInfo contv = null;
       HibernateUtil dh = new HibernateUtil(ContentletVersionInfo.class);
         dh.setQuery("from "+ContentletVersionInfo.class.getName()+" where identifier=? and lang=?");
         dh.setParam(identifier);
         dh.setParam(lang);
         Logger.debug(this.getClass(), "getContentletVersionInfo query: "+dh.getQuery());
         contv = (ContentletVersionInfo)dh.load();
         return contv;
    }

    @Override
    protected void saveContentletVersionInfo(ContentletVersionInfo cvInfo, boolean updateVersionTS) throws DotDataException, DotStateException {
      Identifier ident = APILocator.getIdentifierAPI().find(cvInfo.getIdentifier());
      ContentletVersionInfo vi= null;
      if(ident!=null && InodeUtils.isSet(ident.getId())){
        vi= findContentletVersionInfoInDB(ident.getId(), cvInfo.getLang());
      }
      boolean isNew = vi==null || !InodeUtils.isSet(vi.getIdentifier());
        try {
      BeanUtils.copyProperties(vi, cvInfo);
    } catch (Exception e) {
      throw new DotDataException(e.getMessage());
    }
        if(updateVersionTS){
          vi.setVersionTs(new Date());
        }
      if(isNew) {
            HibernateUtil.save(vi);
        }
        else {
            HibernateUtil.saveOrUpdate(vi);
        }
      HibernateUtil.saveOrUpdate(vi);
        icache.removeContentletVersionInfoToCache(cvInfo.getIdentifier(),cvInfo.getLang());

    }

    @Override
    protected ContentletVersionInfo createContentletVersionInfo(Identifier identifier, long lang, String workingInode) throws DotStateException, DotDataException {
        ContentletVersionInfo cVer=new ContentletVersionInfo();
        cVer.setDeleted(false);
        cVer.setLockedBy(null);
        cVer.setLockedOn(new Date());
        cVer.setIdentifier(identifier.getId());
        cVer.setLang(lang);
        cVer.setWorkingInode(workingInode);
        cVer.setVersionTs(new Date());

        HibernateUtil.save(cVer);
        return cVer;
    }

    @Override
    protected VersionInfo createVersionInfo(Identifier identifier, String workingInode) throws DotStateException, DotDataException {
        Class clazz=UtilMethods.getVersionInfoType(identifier.getAssetType());
        VersionInfo ver;
        try {
            ver = (VersionInfo)clazz.newInstance();
        } catch (Exception e) {
            throw new DotStateException("this shouln't happend");
        }
        ver.setIdentifier(identifier.getId());
        ver.setDeleted(false);
        ver.setLockedBy(null);
        ver.setLockedOn(new Date());
        ver.setWorkingInode(workingInode);
        ver.setVersionTs(new Date());
        HibernateUtil.save(ver);
        return ver;
    }

  @Override
  protected void deleteVersionInfo(String id) throws DotDataException {
    icache.removeVersionInfoFromCache(id);
      VersionInfo info = getVersionInfo(id);
    if(info!=null && UtilMethods.isSet(info.getIdentifier())) {
      String ident = info.getIdentifier();
      HibernateUtil.delete(info);
      icache.removeFromCacheByIdentifier(ident);
    }

  }

  @Override
  protected void deleteContentletVersionInfo(String id, long lang) throws DotDataException {
    HibernateUtil dh = new HibernateUtil(ContentletVersionInfo.class);
        dh.setQuery("from "+ContentletVersionInfo.class.getName()+" where identifier=? and lang=?");
        dh.setParam(id);
        dh.setParam(lang);
        Logger.debug(this.getClass(), "getContentletVersionInfo query: "+dh.getQuery());
        ContentletVersionInfo contv = (ContentletVersionInfo)dh.load();

        if(UtilMethods.isSet(contv.getIdentifier())) {
          HibernateUtil.delete(contv);
          icache.removeContentletVersionInfoToCache(id, lang);
        }
  }
}
TOP

Related Classes of com.dotmarketing.business.VersionableFactoryImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.