/*******************************************************************************
 *
 * Pentaho Big Data
 *
 * Copyright (C) 2019-2022 by Hitachi Vantara : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.big.data.impl.browse.model;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.pentaho.big.data.impl.browse.NamedClusterProvider;
import org.pentaho.di.plugins.fileopensave.api.providers.BaseEntity;
import org.pentaho.di.plugins.fileopensave.api.providers.File;

import java.util.Date;

public class NamedClusterFile extends BaseEntity implements File {
  public static final String TYPE = "file";

  public NamedClusterFile() {
    // Needed for JSON marshalling
  }

  @Override public String getType() {
    return TYPE;
  }

  @Override public String getProvider() {
    return NamedClusterProvider.TYPE;
  }

  public static NamedClusterFile create( String parent, FileObject fileObject ) {
    NamedClusterFile namedClusterFile = new NamedClusterFile();
    namedClusterFile.setName( fileObject.getName().getBaseName() );
    namedClusterFile.setPath( fileObject.getName().getFriendlyURI() );
    namedClusterFile.setParent( parent );
    namedClusterFile.setRoot( NamedClusterProvider.NAME );
    namedClusterFile.setCanEdit( true );
    try {
      namedClusterFile.setDate( new Date( fileObject.getContent().getLastModifiedTime() ) );
    } catch ( FileSystemException ignored ) {
      namedClusterFile.setDate( new Date() );
    }
    return namedClusterFile;
  }

  @Override public boolean equals( Object obj ) {
    // If the object is compared with itself then return true
    if ( obj == this ) {
      return true;
    }

    if ( !( obj instanceof NamedClusterFile ) ) {
      return false;
    }

    NamedClusterFile compare = (NamedClusterFile) obj;
    return compare.getProvider().equals( getProvider() )
      && ( ( compare.getPath() == null && getPath() == null ) || compare.getPath().equals( getPath() ) );
  }

}

@@ -946,16 +946,60 @@
 model;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -2588,16 +2588,101 @@
 e) obj;%0A
+    // This comparison depends on %60getProvider()%60 to always return a hardcoded value%0A
     retu
@@ -2743,63 +2743,27 @@
  && 
-( ( compare.getPath() == null && getPath() == null ) %7C%7C
+StringUtils.equals(
  com
@@ -2776,24 +2776,17 @@
 etPath()
-.equals(
+,
  getPath
@@ -2787,18 +2787,16 @@
 etPath()
- )
  );%0A  %7D%0A

Fix for possible NPE in .equals implementation