/commons/src/main/java/com/photon/phresco/util/ProjectUtils.java

https://github.com/alaksh10/phresco · Java · 120 lines · 85 code · 11 blank · 24 comment · 18 complexity · 003795f8e1593b4e36ca41eee1f0afaf MD5 · raw file

  1. /*
  2. * ###
  3. * Phresco Commons
  4. *
  5. * Copyright (C) 1999 - 2012 Photon Infotech Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ###
  19. */
  20. package com.photon.phresco.util;
  21. import java.io.BufferedReader;
  22. import java.io.BufferedWriter;
  23. import java.io.File;
  24. import java.io.FileReader;
  25. import java.io.FileWriter;
  26. import java.io.IOException;
  27. import java.util.List;
  28. import com.google.gson.Gson;
  29. import com.photon.phresco.exception.PhrescoException;
  30. import com.photon.phresco.model.ModuleGroup;
  31. import com.photon.phresco.model.ProjectInfo;
  32. public class ProjectUtils implements Constants {
  33. public static void writeProjectInfo(ProjectInfo info, File phrescoFolder) throws PhrescoException {
  34. BufferedWriter out = null;
  35. FileWriter fstream = null;
  36. try {
  37. // create .project file inside the .phresco folder
  38. File projectFile = new File(phrescoFolder.getPath() + File.separator + PROJECT_INFO_FILE);
  39. if (!projectFile.exists()) {
  40. projectFile.createNewFile();
  41. }
  42. // make the .phresco folder as hidden for windows
  43. // for linux its enough to create the folder with '.' to make it as
  44. // hidden
  45. if (System.getProperty(OSNAME).startsWith(WINDOWS)) {
  46. Runtime.getRuntime().exec(
  47. "attrib +h " + STR_DOUBLE_QUOTES + phrescoFolder.getPath() + STR_DOUBLE_QUOTES);
  48. }
  49. // write the project info as json string into the .project file
  50. Gson gson = new Gson();
  51. String infoJSON = gson.toJson(info);
  52. fstream = new FileWriter(projectFile.getPath());
  53. out = new BufferedWriter(fstream);
  54. out.write(infoJSON);
  55. } catch (IOException e) {
  56. throw new PhrescoException(e);
  57. } finally {
  58. try {
  59. if (out != null) {
  60. out.close();
  61. }
  62. if (fstream != null) {
  63. fstream.close();
  64. }
  65. } catch (IOException e) {
  66. throw new PhrescoException(e);
  67. }
  68. }
  69. }
  70. public static void updateProjectInfo(ProjectInfo info, File phrescoFolder) throws PhrescoException {
  71. BufferedWriter out = null;
  72. FileWriter fstream = null;
  73. BufferedReader reader = null;
  74. try {
  75. Gson gson = new Gson();
  76. reader = new BufferedReader(new FileReader(phrescoFolder));
  77. ProjectInfo projectInfos = gson.fromJson(reader, ProjectInfo.class);
  78. List<ModuleGroup> ProjectInfomodules = projectInfos.getTechnology().getModules();
  79. List<ModuleGroup> projectInfojsLibraries = projectInfos.getTechnology().getJsLibraries();
  80. List<ModuleGroup> selectedInfomodules = info.getTechnology().getModules();
  81. List<ModuleGroup> selectedInfojsLibraries = info.getTechnology().getJsLibraries();
  82. if(ProjectInfomodules != null && !ProjectInfomodules.isEmpty()) {
  83. selectedInfomodules.addAll(ProjectInfomodules);
  84. }
  85. if(projectInfojsLibraries != null && !projectInfojsLibraries.isEmpty() && selectedInfojsLibraries != null) {
  86. selectedInfojsLibraries.addAll(projectInfojsLibraries);
  87. }
  88. info.getTechnology().setModules(selectedInfomodules);
  89. info.getTechnology().setJsLibraries(selectedInfojsLibraries);
  90. String infoJSON = gson.toJson(info);
  91. fstream = new FileWriter(phrescoFolder.getPath());
  92. out = new BufferedWriter(fstream);
  93. out.write(infoJSON);
  94. } catch (IOException e) {
  95. throw new PhrescoException(e);
  96. } finally {
  97. try {
  98. if (out != null) {
  99. out.close();
  100. }
  101. if (fstream != null) {
  102. fstream.close();
  103. }
  104. } catch (IOException e) {
  105. throw new PhrescoException(e);
  106. }
  107. }
  108. }
  109. }