View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.pages.install;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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.base.Strings;
29  import com.google.common.collect.Lists;
30  import com.google.common.collect.Ordering;
31  import fr.ifremer.quadrige2.synchro.server.application.WebSession;
32  import fr.ifremer.quadrige2.synchro.server.components.progression.ProgressionPanel;
33  import fr.ifremer.quadrige2.synchro.server.config.SynchroServerConfiguration;
34  import fr.ifremer.quadrige2.synchro.server.pages.BasePage;
35  import fr.ifremer.quadrige2.synchro.server.pages.synchro.model.ImportProgressionModel;
36  import fr.ifremer.quadrige2.synchro.server.service.ServiceLocator;
37  import fr.ifremer.quadrige2.synchro.server.service.synchro.job.SynchroNewInstallDbJob;
38  import fr.ifremer.quadrige2.synchro.server.vo.synchro.SynchroJobVO;
39  import fr.ifremer.quadrige2.synchro.vo.SynchroProgressionModel;
40  import fr.ifremer.quadrige2.synchro.vo.SynchroProgressionStatus;
41  import org.apache.commons.collections4.CollectionUtils;
42  import org.apache.commons.io.FileUtils;
43  import org.apache.commons.lang3.ArrayUtils;
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.apache.wicket.AttributeModifier;
47  import org.apache.wicket.ajax.AbstractAjaxTimerBehavior;
48  import org.apache.wicket.ajax.AjaxRequestTarget;
49  import org.apache.wicket.ajax.markup.html.form.AjaxButton;
50  import org.apache.wicket.behavior.AttributeAppender;
51  import org.apache.wicket.markup.html.WebMarkupContainer;
52  import org.apache.wicket.markup.html.basic.Label;
53  import org.apache.wicket.markup.html.form.Form;
54  import org.apache.wicket.markup.html.list.ListItem;
55  import org.apache.wicket.markup.html.list.ListView;
56  import org.apache.wicket.model.*;
57  import org.apache.wicket.model.util.ListModel;
58  import org.apache.wicket.request.mapper.parameter.PageParameters;
59  import org.apache.wicket.util.time.Duration;
60  import org.springframework.core.task.AsyncTaskExecutor;
61  
62  import java.io.File;
63  import java.sql.Timestamp;
64  import java.text.DateFormat;
65  import java.util.Date;
66  import java.util.List;
67  
68  public class InstallPage extends BasePage {
69      private static final long serialVersionUID = 1L;
70  
71      public InstallPage(final PageParameters parameters) {
72          super(parameters);
73  
74          // Create model
75          LoadableDetachableModel<List<File>> fileListModel = new LoadableDetachableModel<List<File>>() {
76              private static final long serialVersionUID = 1L;
77  
78              @Override
79              protected List<File> load() {
80                  return getInstallFiles();
81              }
82          };
83  
84          // List of import jobs
85          ListView<File> fileListView = new ListView<File>("fileList", fileListModel) {
86              private static final long serialVersionUID = 1L;
87  
88              @Override
89              protected void populateItem(ListItem<File> item) {
90                  // File Url
91                  WebMarkupContainer link = new WebMarkupContainer("fileLink");
92                  item.add(link);
93  
94                  link.add(new AttributeModifier("href", new PropertyModel<String>(item.getModel(), "path")));
95  
96                  // File name
97                  link.add(new Label("fileName", new PropertyModel<String>(item.getModel(), "name")));
98              }
99          };
100         add(fileListView);
101     }
102 
103     @Override
104     protected void onConfigure() {
105         super.onConfigure();
106     }
107 
108     /* -- internal methods -- */
109 
110     protected IModel<String> getPageTitleModel() {
111         return new StringResourceModel("install.title", this, null);
112     }
113 
114     protected WebSession getWebSession() {
115         return (WebSession)getSession();
116     }
117 
118 
119 
120     protected List<File> getInstallFiles() {
121         List<File> result = Lists.newArrayList();
122 
123         SynchroServerConfiguration config = SynchroServerConfiguration.getInstance();
124         File synchroDirectory = config.getSynchroDirectory();
125         if (synchroDirectory.exists()) {
126             String baseUrl = "./download/install";
127 
128             File[] files = synchroDirectory.listFiles();
129             if (ArrayUtils.isNotEmpty(files)) {
130                 for (File file: files) {
131                     // Skip directory
132                     if (!file.isDirectory()) {
133                         File urlFile = new File(baseUrl, file.getName());
134                         result.add(urlFile);
135                     }
136                 }
137             }
138         }
139 
140         // Sort by name, using natural order
141         Ordering<File> fileOrdering = Ordering.natural().onResultOf(new Function<File, Comparable>() {
142             public String apply(File file) {
143                 return file.getName();
144             }
145         });
146 
147         return fileOrdering.sortedCopy(result);
148     }
149 }