Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 760a7005cb2e26ede26320e9cd3aa6af478cdaa3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.project;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;

import org.apache.maven.model.Model;

import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.Messages;


/**
 * Project import configuration bean.
 */
public class ProjectImportConfiguration {

  private static final String GROUP_ID = "\\[groupId\\]"; //$NON-NLS-1$

  private static final String ARTIFACT_ID = "\\[artifactId\\]"; //$NON-NLS-1$

  private static final String VERSION = "\\[version\\]"; //$NON-NLS-1$

  /** resolver configuration bean */
  private ResolverConfiguration resolverConfiguration;

  /** the project name template */
  private String projectNameTemplate = ""; //$NON-NLS-1$

  /** Creates a new configuration. */
  public ProjectImportConfiguration(ResolverConfiguration resolverConfiguration) {
    this.resolverConfiguration = resolverConfiguration;
  }

  /** Creates a new configuration. */
  public ProjectImportConfiguration() {
    this(new ResolverConfiguration());
  }

  /** Returns the resolver configuration bean. */
  public ResolverConfiguration getResolverConfiguration() {
    return resolverConfiguration;
  }

  /** Sets the project name template. */
  public void setProjectNameTemplate(String projectNameTemplate) {
    this.projectNameTemplate = projectNameTemplate;
  }

  /** Returns the project name template. */
  public String getProjectNameTemplate() {
    return projectNameTemplate;
  }

  /** 
   * Calculates the project name for the given model.
   * 
   * @deprecated This method does not take into account MavenProjectInfo.basedirRename
   */
  public String getProjectName(Model model) {
    // XXX should use resolved MavenProject or Model
    if(projectNameTemplate.length() == 0) {
      return model.getArtifactId();
    }

    String artifactId = model.getArtifactId();
    String groupId = model.getGroupId();
    if(groupId == null && model.getParent() != null) {
      groupId = model.getParent().getGroupId();
    }
    String version = model.getVersion();
    if(version == null && model.getParent() != null) {
      version = model.getParent().getVersion();
    }

    // XXX needs MavenProjectManager update to resolve groupId and version
    return projectNameTemplate.replaceAll(GROUP_ID, groupId).replaceAll(ARTIFACT_ID, artifactId).replaceAll(VERSION,
        version == null ? "" : version); //$NON-NLS-1$
  }

  /**
   * @deprecated This method does not take into account MavenProjectInfo.basedirRename.
   *    Use IMavenProjectImportResult#getProject instead
   */
  public IProject getProject(IWorkspaceRoot root, Model model) {
    return root.getProject(getProjectName(model));
  }

  /**
   * @deprecated business logic does not belong to a value object
   */
  public IStatus validateProjectName(Model model) {
    String projectName = getProjectName(model); 
    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    // check if the project name is valid
    IStatus nameStatus = workspace.validateName(projectName, IResource.PROJECT);
    if(!nameStatus.isOK()) {
      return nameStatus;
    }

    // check if project already exists
    if(workspace.getRoot().getProject(projectName).exists()) {
      return new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, 0,
          NLS.bind(Messages.importProjectExists, projectName), null); //$NON-NLS-1$
    }
    
    return Status.OK_STATUS;
  }
}

Back to the top