String basePath, boolean initFiles) throws IOException {
if(isProject(projectName)) return getResource(projectName);
IVResource project = createOrionProject(projectName);
IStorage userDir = getUserDirectory();
IStorage projectDir = userDir.newInstance(projectName);
/*
* Load the initial user files extension point and copy the files to the projects root
*/
if(basePath!=null && !basePath.equals("")){
project.create(basePath + "/");
}
if(initFiles){
List<?> extensions = ServerManager.getServerManager().getExtensions(IDavinciServerConstants.EXTENSION_POINT_INITIAL_USER_FILES, IDavinciServerConstants.EP_TAG_INITIAL_USER_FILE);
for (Iterator<?> iterator = extensions.iterator(); iterator.hasNext();) {
IConfigurationElement libraryElement = (IConfigurationElement) iterator.next();
String path = libraryElement.getAttribute(IDavinciServerConstants.EP_ATTR_INITIAL_USER_FILE_PATH);
String name = libraryElement.getDeclaringExtension().getContributor().getName();
Bundle bundle = Activator.getActivator().getOtherBundle(name);
IStorage file =this.getStorage().newInstance(project.getPath());
if(basePath!=null && !basePath.equals(""))
file = file.newInstance(project.getPath()+ "/" + basePath);
VResourceUtils.copyDirectory(file, path, bundle);
}
IStorage basePathDir;
if(basePath!=null && !basePath.equals("")){
basePathDir = projectDir.newInstance(projectDir, basePath);
}else{
basePathDir = projectDir;
}
if(projectTemplateDirectoryName!=null && !projectTemplateDirectoryName.equals("")){
IProjectTemplatesManager projectTemplatesManager = ServerManager.getServerManager().getProjectTemplatesManager();
IStorage projectTemplatesDirectory = projectTemplatesManager.getProjectTemplatesDirectory();
IStorage templateDir = projectTemplatesDirectory.newInstance(projectTemplatesDirectory, projectTemplateDirectoryName);
if(templateDir.exists()) {
IStorage[] files = templateDir.listFiles();
for (int i = 0; i < files.length; i++) {
IStorage file = files[i];
if(file.isDirectory() && file.getName().equals(IDavinciServerConstants.DOT_SETTINGS)){
IStorage destinationDir = projectDir.newInstance(projectDir, IDavinciServerConstants.DOT_SETTINGS);
destinationDir.mkdirs();
IStorage[] dotSettingsFiles = file.listFiles();
for(int j = 0; j < dotSettingsFiles.length; j++){
IStorage settingsFile = dotSettingsFiles[j];
IStorage destination = file.newInstance(file, settingsFile.getName());
if(settingsFile.getName().equals(IDavinciServerConstants.LIBS_SETTINGS) &&
basePath!=null && !basePath.equals("")){
// If creating an Eclipse project, add "WebContent/" into library paths in libs.settings before writing out to the template.
copyFileAddWebContent(settingsFile, destination);
}else{
copyFile(settingsFile, destination);
}
}
}else if (file.isFile()) {
IStorage destination = basePathDir.newInstance(basePathDir, file.getName());
copyFile(file, destination);
} else if (file.isDirectory()) {
IStorage destination = basePathDir.newInstance(basePathDir, file.getName());
copyDirectory(file, destination);
}
}
}
}
if(projectToClone!=null && !projectToClone.equals("")){
IStorage projectToCloneDir = userDir.newInstance(projectToClone);
IStorage webContentDir = projectToCloneDir.newInstance(projectToCloneDir, IDavinciServerConstants.WEBCONTENT);
Boolean oldProjectIsEclipse = webContentDir.exists();
Boolean newProjectIsEclipse = (basePath!=null && !basePath.equals(""));
if(projectToCloneDir.exists()) {
IStorage[] files = projectToCloneDir.listFiles();
for (int i = 0; i < files.length; i++) {
IStorage file = files[i];
String filename = file.getPath();
IPath path = new Path(filename);
if(file.isFile() && path.segmentCount() > 0 && path.segment(1).equals(IDavinciServerConstants.DOT_PROJECT)){
// Eclipse projects have a .project file. Don't copy the cloned project's .project file
// into the new project - if the new project is an Eclipse project, other code adds the .project file.
continue;
}else if(file.isDirectory() && file.getName().equals(IDavinciServerConstants.DOT_SETTINGS)){
IStorage destinationDir = projectDir.newInstance(projectDir, IDavinciServerConstants.DOT_SETTINGS);
destinationDir.mkdirs();
IStorage[] dotSettingsFiles = file.listFiles();
for(int j = 0; j < dotSettingsFiles.length; j++){
IStorage settingsFile = dotSettingsFiles[j];
if(settingsFile.getName().equals(IDavinciServerConstants.LIBS_SETTINGS)){
IStorage destination = destinationDir.newInstance(destinationDir, settingsFile.getName());
if(!oldProjectIsEclipse && newProjectIsEclipse){
copyFileAddWebContent(settingsFile, destination);
}else if(oldProjectIsEclipse && !newProjectIsEclipse){
copyFileStripWebContent(settingsFile, destination);
}else{
copyFile(settingsFile, destination);
}
}
}
}else if(file.isDirectory() && path.segmentCount() > 1 && path.segment(1).equals(IDavinciServerConstants.WEBCONTENT)){
// Copy the contents of WebContent/* into the base folder for the new project
copyDirectory(file, basePathDir);
}else if (file.isFile()) {
IStorage destination = basePathDir.newInstance(basePathDir, file.getName());
copyFile(file, destination);
} else if (file.isDirectory()) {
IStorage destination = basePathDir.newInstance(basePathDir, file.getName());
copyDirectory(file, destination);
}
}
}
}