View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.pages.install;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import com.google.common.base.Function;
28  import com.google.common.collect.Lists;
29  import com.google.common.collect.Ordering;
30  import fr.ifremer.quadrige3.synchro.server.application.WebSession;
31  import fr.ifremer.quadrige3.synchro.server.config.SynchroServerConfiguration;
32  import fr.ifremer.quadrige3.synchro.server.pages.BasePage;
33  import org.apache.commons.lang3.ArrayUtils;
34  import org.apache.wicket.AttributeModifier;
35  import org.apache.wicket.markup.html.WebMarkupContainer;
36  import org.apache.wicket.markup.html.basic.Label;
37  import org.apache.wicket.markup.html.list.ListItem;
38  import org.apache.wicket.markup.html.list.ListView;
39  import org.apache.wicket.model.IModel;
40  import org.apache.wicket.model.LoadableDetachableModel;
41  import org.apache.wicket.model.PropertyModel;
42  import org.apache.wicket.model.StringResourceModel;
43  import org.apache.wicket.request.mapper.parameter.PageParameters;
44  
45  import java.io.File;
46  import java.util.List;
47  
48  public class InstallPage extends BasePage {
49      private static final long serialVersionUID = 1L;
50  
51      public InstallPage(final PageParameters parameters) {
52          super(parameters);
53  
54          // Create model
55          LoadableDetachableModel<List<File>> fileListModel = new LoadableDetachableModel<List<File>>() {
56              private static final long serialVersionUID = 1L;
57  
58              @Override
59              protected List<File> load() {
60                  return getInstallFiles();
61              }
62          };
63  
64          // List of import jobs
65          ListView<File> fileListView = new ListView<File>("fileList", fileListModel) {
66              private static final long serialVersionUID = 1L;
67  
68              @Override
69              protected void populateItem(ListItem<File> item) {
70                  // File Url
71                  WebMarkupContainer link = new WebMarkupContainer("fileLink");
72                  item.add(link);
73  
74                  link.add(new AttributeModifier("href", new PropertyModel<String>(item.getModel(), "path")));
75  
76                  // File name
77                  link.add(new Label("fileName", new PropertyModel<String>(item.getModel(), "name")));
78              }
79          };
80          add(fileListView);
81      }
82  
83      /* -- internal methods -- */
84  
85      protected IModel<String> getPageTitleModel() {
86          return new StringResourceModel("install.title", this, null);
87      }
88  
89      protected WebSession getWebSession() {
90          return (WebSession)getSession();
91      }
92  
93  
94  
95      protected List<File> getInstallFiles() {
96          List<File> result = Lists.newArrayList();
97  
98          SynchroServerConfiguration config = SynchroServerConfiguration.getInstance();
99          File synchroDirectory = config.getSynchroDirectory();
100         if (synchroDirectory.exists()) {
101             String baseUrl = "./download/install";
102 
103             File[] files = synchroDirectory.listFiles();
104             if (ArrayUtils.isNotEmpty(files)) {
105                 for (File file: files) {
106                     // Skip directory
107                     if (!file.isDirectory()) {
108                         File urlFile = new File(baseUrl, file.getName());
109                         result.add(urlFile);
110                     }
111                 }
112             }
113         }
114 
115         // Sort by name, using natural order
116         Ordering<File> fileOrdering = Ordering.natural().onResultOf(new Function<File, Comparable>() {
117             public String apply(File file) {
118                 return file.getName();
119             }
120         });
121 
122         return fileOrdering.sortedCopy(result);
123     }
124 }