Esempio n. 1
0
  @SuppressWarnings({"unchecked", "deprecation"})
  public boolean renameFile(File file, String newName)
      throws DotStateException, DotDataException, DotSecurityException {

    // getting old file properties
    String oldFileName = file.getFileName();
    String ext = UtilMethods.getFileExtension(oldFileName);
    Folder folder =
        APILocator.getFolderAPI()
            .findParentFolder(file, APILocator.getUserAPI().getSystemUser(), false);

    Identifier ident = APILocator.getIdentifierAPI().find(file);

    String newFileName = newName;
    if (UtilMethods.isSet(ext)) {
      newFileName = newFileName + "." + ext;
    }

    if (fileNameExists(folder, newFileName) || file.isLocked()) return false;

    List<Versionable> versions = APILocator.getVersionableAPI().findAllVersions(ident);

    boolean islive = false;

    for (Versionable version : versions) {
      File f = (File) version;

      // sets filename for this new file
      f.setFileName(newFileName);

      HibernateUtil.saveOrUpdate(f);

      if (f.isLive()) islive = true;
    }

    LiveCache.removeAssetFromCache(file);
    WorkingCache.removeAssetFromCache(file);
    CacheLocator.getIdentifierCache().removeFromCacheByVersionable(file);

    ident.setURI(APILocator.getIdentifierAPI().find(folder).getPath() + newFileName);
    // HibernateUtil.saveOrUpdate(ident);
    APILocator.getIdentifierAPI().save(ident);

    if (islive) {
      LiveCache.removeAssetFromCache(file);
      LiveCache.addToLiveAssetToCache(file);
    }
    WorkingCache.removeAssetFromCache(file);
    WorkingCache.addToWorkingAssetToCache(file);
    CacheLocator.getIdentifierCache().removeFromCacheByVersionable(file);

    // RefreshMenus.deleteMenus();
    RefreshMenus.deleteMenu(file);
    CacheLocator.getNavToolCache().removeNav(folder.getHostId(), folder.getInode());

    return true;
  }
Esempio n. 2
0
  /**
   * Moves a file into the given directory OR host
   *
   * @param file File to be moved
   * @param parent Destination Folder
   * @param host Destination Host
   * @return true if move success, false otherwise
   * @throws DotDataException
   * @throws DotStateException
   * @throws DotSecurityException
   */
  private Boolean moveFile(File file, Folder parent, Host host)
      throws DotStateException, DotDataException, DotSecurityException {

    HostAPI hostAPI = APILocator.getHostAPI();

    // Find file identifier
    Identifier identifier = com.dotmarketing.business.APILocator.getIdentifierAPI().find(file);

    // gets working container
    File workingWebAsset =
        (File)
            APILocator.getVersionableAPI()
                .findWorkingVersion(identifier, APILocator.getUserAPI().getSystemUser(), false);
    // gets live container
    File liveWebAsset =
        (File)
            APILocator.getVersionableAPI()
                .findLiveVersion(identifier, APILocator.getUserAPI().getSystemUser(), false);

    // checks if another identifer with the same name exists in the same
    Boolean fileNameExists;
    if (parent != null) {
      fileNameExists = fileNameExists(parent, file.getFileName());
    } else {
      fileNameExists =
          fileNameExists(APILocator.getFolderAPI().findSystemFolder(), file.getFileName());
    }
    if (fileNameExists) {
      return false;
    }

    // assets cache
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
      LiveCache.removeAssetFromCache(liveWebAsset);
    }
    WorkingCache.removeAssetFromCache(workingWebAsset);

    // gets old parent
    Folder oldParent =
        APILocator.getFolderAPI()
            .findParentFolder(workingWebAsset, APILocator.getUserAPI().getSystemUser(), false);

    /*oldParent.deleteChild(workingWebAsset);
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
        oldParent.deleteChild(liveWebAsset);
    }
    //add new Parent
    parent.addChild(workingWebAsset);
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
        parent.addChild(liveWebAsset);
    }*/

    // gets identifier for this webasset and changes the uri and persists it
    User systemUser;
    try {
      systemUser = APILocator.getUserAPI().getSystemUser();
      if (host == null) {
        host = hostAPI.findParentHost(parent, systemUser, false);
      }
    } catch (DotDataException e) {
      Logger.error(FileFactory.class, e.getMessage(), e);
      throw new DotRuntimeException(e.getMessage(), e);

    } catch (DotSecurityException e) {
      Logger.error(FileFactory.class, e.getMessage(), e);
      throw new DotRuntimeException(e.getMessage(), e);
    }
    identifier.setHostId(host.getIdentifier());
    identifier.setURI(parent != null ? workingWebAsset.getURI(parent) : workingWebAsset.getURI());
    // HibernateUtil.saveOrUpdate(identifier);
    APILocator.getIdentifierAPI().save(identifier);

    if (UtilMethods.isSet(liveWebAsset))
      CacheLocator.getIdentifierCache().removeFromCacheByVersionable(liveWebAsset);
    //		IdentifierCache.addAssetToIdentifierCache(liveWebAsset);

    // Add to Preview and Live Cache
    if ((liveWebAsset != null) && (InodeUtils.isSet(liveWebAsset.getInode()))) {
      LiveCache.removeAssetFromCache(liveWebAsset);
      LiveCache.addToLiveAssetToCache(liveWebAsset);
    }
    WorkingCache.removeAssetFromCache(workingWebAsset);
    WorkingCache.addToWorkingAssetToCache(workingWebAsset);

    if (file.isShowOnMenu()) {
      // existing folder with different show on menu ... need to regenerate menu
      if (parent != null) {
        RefreshMenus.deleteMenu(oldParent, parent);
        CacheLocator.getNavToolCache().removeNav(parent.getHostId(), parent.getInode());
      } else {
        RefreshMenus.deleteMenu(oldParent);
      }
      CacheLocator.getNavToolCache().removeNav(oldParent.getHostId(), oldParent.getInode());
    }

    return true;
  }