b681547d63d3760be4f42cfc00642a409ca10c05
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / helpServer / impl / src / main / java / com / highstreet / technologies / helpserver / data / HelpInfrastructureObject.java
1 package com.highstreet.technologies.helpserver.data;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URISyntaxException;
6 import java.nio.file.Path;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Comparator;
10
11 import org.json.JSONObject;
12 import org.osgi.framework.Bundle;
13 import org.osgi.framework.FrameworkUtil;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class HelpInfrastructureObject extends JSONObject {
18
19         private static final Logger LOG = LoggerFactory.getLogger(HelpInfrastructureObject.class);
20         private static String HELPBASE = "help";
21         private static String KARAFBUNDLERESOURCEHELPROOT = "/"+HELPBASE;
22         private static String KARAFHELPDIRPREFIX = "data/cache/com.highstreet.technologies.";
23         public static File KARAFHELPDIRECTORY = new File(KARAFHELPDIRPREFIX+HELPBASE);
24
25         public static class VersionObject extends JSONObject
26         {
27                 private static Comparator<VersionObject> comp;
28                 private final String mVersion;
29                 public String getVersion() {return this.mVersion;}
30                 public VersionObject(String path,String date,String label,String version)
31                 {
32                         this.mVersion=version;
33                         this.put("path", path);
34                         this.put("date",date);
35                         this.put("label", label);
36                 }
37                 public static Comparator<VersionObject> getComparer() {
38                         if(comp==null)
39                                 comp=new Comparator<HelpInfrastructureObject.VersionObject>() {
40
41                                         @Override
42                                         public int compare(VersionObject o1, VersionObject o2) {
43                                                 return o1.getVersion().compareTo(o2.getVersion());
44                                         }
45                                 };
46                                 return comp;
47                 }
48                 public VersionObject cloneAsLatest() {
49                         return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"), "latest");
50                 }
51                 public VersionObject cloneAsCurrent() {
52                         return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"), "current");
53                 }
54         }
55         public static class NodeObject extends JSONObject
56         {
57                 public NodeObject(Path base,File dir,String label,ArrayList<VersionObject> versions)
58                 {
59                         this.put("label", label);
60                         if(versions!=null && versions.size()>0)
61                         {
62                                 JSONObject o=new JSONObject();
63                                 this.put("versions", o);
64                                 for(VersionObject version : versions)
65                                         o.put(version.getVersion(), version);
66
67                         }
68                         File[] list = dir.listFiles();
69                      if (list == null) return;
70                      for(File f: list)
71                      {
72                          if(f.isDirectory())
73                          {
74                                  ArrayList<VersionObject> versions2=findReadmeVersionFolders(base, f.toPath(),true);
75                                  if(versions2!=null && versions2.size()>0)
76                                  {
77                                          JSONObject nodes;
78                                          if(!this.has("nodes"))
79                                                  this.put("nodes",new JSONObject());
80                                          nodes=this.getJSONObject("nodes");
81
82                                          NodeObject o=new NodeObject(base,f,f.getName(),versions2);
83                                          nodes.put(o.getString("label").toLowerCase(),o);
84                                  }
85                          }
86                      }
87                 }
88
89         }
90         public HelpInfrastructureObject(Path proot) throws URISyntaxException
91         {
92                  File root = proot.toFile();
93              File[] list = root.listFiles();
94              if (list == null) return;
95              for(File f: list)
96              {
97                  if(f.isDirectory())
98                  {
99                          ArrayList<VersionObject> versions=findReadmeVersionFolders(root.toPath(), f.toPath(),true);
100                          if(versions!=null && versions.size()>0)
101                          {
102                                  NodeObject o=new NodeObject(proot,f,f.getName(),versions);
103                                  this.put(o.getString("label").toLowerCase(), o);
104                          }
105                  }
106              }
107
108
109         }
110          public static void walk(ArrayList<File> results, String path ) {
111
112                 File root = new File( path );
113                 File[] list = root.listFiles();
114
115                 if (list == null) return;
116
117                 for ( File f : list ) {
118                     if ( f.isDirectory() ) {
119                         walk(results, f.getAbsolutePath() );
120                         //System.out.println( "Dir:" + f.getAbsoluteFile() );
121                     }
122                     else {
123                         //System.out.println( "File:" + f.getAbsoluteFile() );
124                         if(f.isFile() && f.getName().endsWith(".md") )
125                                 results.add(f);
126                     }
127                 }
128             }
129          private static ArrayList<VersionObject> findReadmeVersionFolders(Path base,Path root,boolean appendCurrent)
130          {
131                  ArrayList<VersionObject> list=new ArrayList<>();
132                  File[] files=root.toFile().listFiles();
133                  int baselen=base.toFile().getAbsolutePath().length();
134                  if(files!=null)
135                  {
136                          for(File f : files)
137                          {
138                                 if(f.isDirectory() && new File(f.getAbsolutePath()+"/README.md").exists())
139                                         list.add(new VersionObject(f.getAbsolutePath().substring(baselen+1)+"/README.md","","",f.getName()));
140                          }
141                  }
142                  Collections.sort(list, VersionObject.getComparer());
143                  Collections.reverse(list);
144                  if(list.size()>0 && appendCurrent)
145                  {
146                          list.add(list.get(0).cloneAsCurrent());
147                  }
148                  return list;
149          }
150
151
152          public static void createFilesFromResources()
153          {
154
155                  if (KARAFHELPDIRECTORY.exists()) {
156                          LOG.info("Delete existing directory");
157                          try {
158                                  ExtactBundleResource.deleteRecursively(KARAFHELPDIRECTORY);
159                          } catch (IOException e1) {
160                                  LOG.warn(e1.toString());
161                          }
162                  }
163
164                  LOG.info("Extract");
165                  try {
166                          Bundle b=FrameworkUtil.getBundle(HelpInfrastructureObject.class);
167                          if (b == null) {
168                                  LOG.info("No bundlereference: Use target in filesystem.");
169                                  //URL helpRessource = JarFileUtils.stringToJarURL("target/helpserver-impl-0.4.0-SNAPSHOT.jar",KARAFBUNDLERESOURCEHELPROOT);
170
171                          } else {
172                                  LOG.info("Bundle location:{} State:{}",b.getLocation(), b.getState());
173                                  LOG.info("Write files from Resource");
174                                  ExtactBundleResource.copyBundleResoucesRecursively(b,"data/cache/com.highstreet.technologies." ,KARAFBUNDLERESOURCEHELPROOT);
175                          }
176                  } catch (IOException e) {
177                          LOG.warn("No help files available. Exception: "+e.toString());
178                  }
179          }
180
181          public static Path getHelpDirectoryBase() {
182                  return(KARAFHELPDIRECTORY.toPath());
183          }
184 }