/**
   * Removes the element reference for the specified element, rebuild later
   *
   * @param element A method element.
   */
  private void removeReference(MethodElement element) {
    PackageDependency dependency = null;

    MethodElement pkg = (MethodElement) LibraryUtil.getSelectable(element);
    if ((pkg == null) || ((dependency = getDependency(pkg, false)) == null)) {
      return;
    }

    dependency.removeReference(element);
  }
  /**
   * Builds the dependency for a method element.
   *
   * @param element A method element.
   */
  private void buildDependency(MethodElement element) {
    if (element == null) {
      return;
    }

    try {
      PackageDependency dependency = buildDependencyFor(element);
      boolean isProcess = element instanceof ProcessComponent;

      EList elements = element.eContents();
      if (elements != null) {
        for (Iterator it = elements.iterator(); it.hasNext(); ) {
          Object obj = it.next();
          if (!(obj instanceof MethodElement)) {
            continue;
          }
          MethodElement methodElement = (MethodElement) obj;
          if (methodElement != null && !LibraryUtil.selectable(methodElement)) {
            buildDependencyFor(methodElement);
          } else if (isProcess && methodElement instanceof ProcessPackage) {
            for (Iterator itt = methodElement.eAllContents(); itt.hasNext(); ) {
              Object objj = itt.next();
              if (objj instanceof MethodElement) {
                MethodElement m = (MethodElement) objj;
                if (!LibraryUtil.selectable(m)) {
                  buildDependencyFor(m);
                }
              }
            }
          }
        }
      }

      dependency.setLoaded(true);
    } catch (Exception e) {
      if (debug) {
        e.printStackTrace();
      }
    }
  }
  /**
   * Returns the package dependency for a method element.
   *
   * @param element A method element.
   * @return A <code>PackageDependency</code>.
   */
  public PackageDependency getDependency(MethodElement element) {
    if (!LibraryUtil.selectable(element)) {
      return null;
    }

    // Always rebuild the dependency for the element since the
    // dependents may not be fully established.
    PackageDependency dependency = getDependency(element, false);
    if (dependency == null || !dependency.isLoaded()) {
      buildDependency(element);
    }

    return getDependency(element, false);
  }
  private PackageDependency getDependency(MethodElement element, boolean create) {
    if (!LibraryUtil.selectable(element)) {
      if (debug) {
        System.out.println("Error, Selectable element required: " + element); // $NON-NLS-1$
      }
      return null;
    }

    PackageDependency dependency = (PackageDependency) dependencyMap.get(element);
    if (dependency == null && create) {
      dependency = new PackageDependency(element);
      dependencyMap.put(element, dependency);
    }

    return dependency;
  }
  /** Analyzes the imported library with respect to the base library. */
  public void analyze(IProgressMonitor monitor) {
    try {
      if (monitor != null) {
        monitor.setTaskName(ImportResources.ConfigurationImportService_MSG0);
      }

      data.getErrorInfo().clear();

      // Prepare the library files.
      String path = data.llData.getParentFolder();
      if (path.indexOf(File.separator + LibraryDocument.libraryFile) < 0) {
        path += File.separator + LibraryDocument.libraryFile;
      }
      File importingLibPath = new File(path);

      boolean isLibraryFile = true;
      if (!importingLibPath.exists()) {
        importingLibPath = new File(importingLibPath.getParentFile(), LibraryDocument.exportFile);
        isLibraryFile = false;
      }

      if (!importingLibPath.exists()) {
        data.getErrorInfo()
            .addError(
                NLS.bind(
                    ImportResources.ConfigurationImportService_MSG1, importingLibPath.getParent()));
        return;
      }

      boolean handleVersion = isLibraryFile;
      if (handleVersion) {
        upGradeInfo =
            new ConfigurationImportService.UpgradeInfo(
                UpgradeCallerInfo.upgradeImportConfig, importingLibPath);
        if (!handleToolVersion(importingLibPath, upGradeInfo)) {
          String errMsg = upGradeInfo.getErrorMsg();
          if (errMsg == null || errMsg.length() == 0) {
            errMsg = ImportResources.ImportConfigurationWizard_ERR_Import_configuration;
          }
          data.getErrorInfo().addError(NLS.bind(errMsg, importingLibPath.getParent()));
          return;
        }
        if (upGradeInfo.getCopiedLibFile() != null) {
          importingLibPath = upGradeInfo.getCopiedLibFile();
        }
      }

      importingLibDoc = new LibraryDocument(importingLibPath);

      boolean isConfigSpecs = importingLibDoc.isConfigSpecsOnly();

      if (isConfigSpecs) {

        specsMgr = new ConfigSpecsImportManager();

        // Scan the library file for configuration information.
        data.specs = specsMgr.getConfigSpecs(importingLibDoc);

      } else {
        if (!isLibraryFile) {
          data.getErrorInfo()
              .addError(
                  NLS.bind(
                      ImportResources.ConfigurationImportService_MSG1,
                      importingLibPath.getParent()));
          return;
        }
        data.specs = null;

        // Open the library and compare the difference.

        // need to open and close the library to have the project resources initialized properly
        String libDir = importingLibPath.getParentFile().getAbsolutePath();

        String projectName =
            "Configuration Import Project (" //$NON-NLS-1$
                + Integer.toHexString(libDir.hashCode())
                + ")"; //$NON-NLS-1$

        MethodLibraryProject.openProject(libDir, projectName, monitor);
        try {
          MethodLibrary importLibraty = LibraryUtil.loadLibrary(importingLibPath.getAbsolutePath());
          MethodLibrary baseLibrary = LibraryService.getInstance().getCurrentMethodLibrary();

          handleTypeChanges(baseLibrary, importLibraty);

          String baseLibDir = null;
          try { // Not neccessary, but prevent introducing any potential regression due to this
                // change
            File bFile = new File(baseLibrary.eResource().getURI().toFileString()).getParentFile();
            ;
            baseLibDir = bFile.getAbsolutePath();
          } catch (Throwable e) {
          }
          if (libDir.equalsIgnoreCase(baseLibDir)) {
            data.getErrorInfo()
                .addError(
                    NLS.bind(
                        ImportResources.ConfigurationImportService_MSG2,
                        importingLibPath.getParent()));
            return;
          }

          fixImportLibrarySystemPackageGUIDs(baseLibrary, importLibraty);

          diffMgr = new LibraryDiffManager(baseLibrary, importLibraty);
          diffMgr.buildDiffTree();

          if (localDebug) {
            diffMgr.rootDiffTree.debugDump();
          }

        } catch (RuntimeException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        MethodLibraryProject.closeProject(libDir, monitor);
        MethodLibraryProject.deleteProject(libDir, monitor);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  /**
   * Generates the HTML for a Method element.
   *
   * @param layout A Method Element. Layout object.
   * @param linkedElements If not null, this object will be populated with a list of linked elements
   *     in the page.
   * @return A url of the generated content HTML file.
   */
  public String generateHtml(IElementLayout layout) {
    if (layout == null) {
      return "about:blank"; //$NON-NLS-1$
    }

    // System.out.println("*** generateHtml: " + LibraryUtil.getTypeName(layout.getElement()) );

    // add time logging when publishing element
    long time_start = Calendar.getInstance().getTimeInMillis();
    String elementPath = layout.getNoAdjustedElementPath().replace('/', File.separatorChar);
    String elementPathName = elementPath + layout.getFileName(ResourceHelper.FILE_EXT_HTML);
    String filePath = this.getPublishDir() + elementPath;
    String html_file = this.getPublishDir() + elementPathName;

    try {

      StringBuffer xml = getXml(layout);

      String xsl_uri;

      File f = new File(filePath);
      if (!f.exists()) {
        f.mkdirs();
      }

      // Generate the additonal outputs.
      List layouts = layout.getLayouts();
      xsl_uri = layoutXslRootPath + layout.getXslUrl();
      generateHtml(layout, xsl_uri, html_file, xml);

      // Generate other layout files.
      if (layouts != null && layouts.size() > 0) {
        for (Iterator it = layouts.iterator(); it.hasNext(); ) {
          LayoutInfo info = (LayoutInfo) it.next();
          xsl_uri = layoutXslRootPath + info.layout_xsl;
          String file = filePath + info.fileName;
          generateHtml(layout, xsl_uri, file, xml);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      html_file = null;
    }

    long time_end = Calendar.getInstance().getTimeInMillis();
    long mini_seconds = time_end - time_start;
    if (mini_seconds > 1000) {
      String msg =
          mini_seconds
              + " mini-second(s) publishing element "
              + LibraryUtil.getTypeName(layout.getElement())
              + "["
              + elementPathName
              + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      IContentValidator validator = getValidator();
      if (validator == null) {
        System.out.println(msg);
      } else {
        validator.logInfo(msg);
      }
    }
    if (html_file != null) {
      return html_file;
    }

    return "about:blank"; //$NON-NLS-1$
  }
  /**
   * Builds the dependency for a method element.
   *
   * <p>This creates an <code>ElementReference</code> for the given element that points to all the
   * referenced elements, and adds the given element as a dependent element to all the referenced
   * elements. This should only be called once for each method element.
   *
   * @param element A method element.
   * @return A <code>PackageDependency</code>.
   */
  private PackageDependency buildDependencyFor(MethodElement element) {
    if (element == null) {
      return null;
    }

    IConfigurationManager configMgr = LibraryService.getInstance().getConfigurationManager(config);
    if (configMgr != null) {
      SupportingElementData seData = configMgr.getSupportingElementData();
      if (seData != null && seData.isEnabled()) {
        seData.processVariabilityChildren(element, null);
      }
    }

    // Build the dependency on the selectable element/parent only
    MethodElement selectableElement = (MethodElement) LibraryUtil.getSelectable(element);
    if (selectableElement == null) {
      return null;
    }

    PackageDependency dependency = getDependency(selectableElement, true);

    // Remove any existing element reference for this element.
    dependency.removeReference(element);
    /*
    		// Get the VariabilityElement.
    		ContentElement baseElement = null;
    		if (element instanceof ContentElement) {
    			baseElement = (ContentElement) ((ContentElement) element)
    					.getVariabilityBasedOnElement();
    			if (baseElement != null) {
    				// Establish the package reference.
    				EObject selectableBase = LibraryUtil.getSelectable(baseElement);
    				if (selectableBase != null) {
    					PackageReference pkgRef = dependency.getReference(
    							selectableBase, true);

    					if (!pkgRef.hasReference(element, baseElement)) {
    						// Add the detail element reference to the package
    						// reference.
    						VariabilityElementReference ref = new VariabilityElementReference(
    								element, baseElement);
    						pkgRef.addReference(ref);
    					}

    					// Set the dependent of the referenced package.
    					getDependency(selectableBase, true).addDependent(
    							selectableElement);
    				}
    			}
    		}

    		List references = element.eCrossReferences();

    		// Update the dependents of those elements in the list.
    		if (references != null && references.size() > 0) {
    			// Get the selectable references
    			for (Iterator it = references.iterator(); it.hasNext();) {
    				EObject refElement = (EObject) it.next();
    				EObject selectableRef = LibraryUtil.getSelectable(refElement);
    				if (selectableRef != null) {
    					PackageReference pkgRef = dependency.getReference(
    							selectableRef, true);

    					if (element == selectableElement
    							&& refElement == selectableRef) {
    						// No need to add this.
    						continue;
    					}

    					if (!pkgRef.hasReference(element, refElement)) {
    						GeneralReference ref = new GeneralReference(element,
    								refElement);
    						pkgRef.addReference(ref);
    					}

    					getDependency(selectableRef, true).addDependent(
    							selectableElement);
    				}
    			}
    		}
    */
    List properties = LibraryUtil.getStructuralFeatures(element, true);
    for (int i = 0; i < properties.size(); i++) {
      EStructuralFeature f = (EStructuralFeature) properties.get(i);
      if (!(f instanceof EReference)) {
        continue;
      }

      EReference feature = (EReference) f;
      if (feature.isContainer() || feature.isContainment()) {
        continue;
      }

      if (element instanceof Task) {
        if (feature == UmaPackage.eINSTANCE.getTask_Steps()) {
          continue;
        }
      }

      if (element instanceof TaskDescriptor) {
        if (feature == UmaPackage.eINSTANCE.getTaskDescriptor_SelectedSteps()) {
          continue;
        }
      }

      Object value = TypeDefUtil.getInstance().eGet(element, feature);
      if (value == null) {
        continue;
      }

      MethodElement refElement = null;
      List values = null;
      int count = 0;

      if (feature.isMany()) {
        values = (List) value;
        if (values.size() > 0) {
          refElement = (MethodElement) values.get(count);
        }
      } else if (value instanceof MethodElement) {
        refElement = (MethodElement) value;

        if (replacerSet != null) {
          if (feature == UmaPackage.eINSTANCE.getVariabilityElement_VariabilityBasedOnElement()) {
            VariabilityElement ve =
                element instanceof VariabilityElement ? (VariabilityElement) element : null;
            VariabilityType type = ve == null ? null : ve.getVariabilityType();
            if (type == VariabilityType.EXTENDS_REPLACES || type == VariabilityType.REPLACES) {
              replacerSet.add(ve);
            }
          }
        }
      }

      while (refElement != null) {

        boolean skip = false;
        if (feature == UmaPackage.eINSTANCE.getRole_Modifies()) {
          skip = true;
        }

        MethodElement selectableRef =
            skip ? null : (MethodElement) LibraryUtil.getSelectable(refElement);
        if (selectableRef != null) {
          PackageReference pkgRef = dependency.getReference(selectableRef, true);

          if (element == selectableElement && refElement == selectableRef) {
            // No need to add this.
            break;
          }

          ElementReference ref = pkgRef.getReference(element, refElement);
          if (ref == null) {
            ref = new ElementReference(element, refElement);
            pkgRef.addReference(ref);
          }

          ref.addFeature(feature);

          getDependency(selectableRef, true).addDependent(selectableElement);
        }

        refElement = null;
        if (values != null) {
          count++;
          if (count < values.size()) {
            refElement = (MethodElement) values.get(count);
          }
        }
      }
    }

    return dependency;
  }