BrightSide Workbench Full Report + Source Code
WebResource.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.web.context;
20 
21 import java.io.File;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27 import java.util.TreeMap;
28 import org.turro.string.ObjectString;
29 
34 public class WebResource {
35 
36  private File resource;
37 
38  // Modified
39  private final TreeMap<String, File> editables = new TreeMap<>();
40 
41  public void setResource(File file) {
42  this.resource = file;
43  }
44 
45  public File getResource() {
46  return resource;
47  }
48 
49  public void addEditable(String modified, File file) {
50  editables.put(modified, file);
51  }
52 
53  public Map<String, File> getEditables() {
54  return editables;
55  }
56 
57  public File getCurrentEditable() {
58  return editables.lastEntry().getValue();
59  }
60 
61  public void addFile(WebResourceParts wrp, File file) {
62  if(!wrp.isEmpty()) {
63  if(wrp.isResource()) {
64  setResource(file);
65  } else {
66  addEditable(wrp.getModified(), file);
67  }
68  }
69  }
70 
71  /* Utils */
72 
73  public static WebResourceParts decompose(File file) {
74  return new WebResourceParts(file);
75  }
76 
77  private static final Long MAX_DIFFERENCE = (1000L * 60L * 60L * 3 /* hours */);
78 
79  public void cleanEditables(String current) {
80  Set<String> toRemove = new HashSet<>();
81  Long previous = ((Date) ObjectString.parseString(current,
82  ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false)).getTime();
83  for(String modified : editables.descendingKeySet()) {
84  long modification = ((Date) ObjectString.parseString(modified,
85  ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false)).getTime();
86  if(previous != null) {
87  if(previous - modification < MAX_DIFFERENCE) {
88  toRemove.add(modified);
89  }
90  }
91  previous = modification;
92  }
93  toRemove.forEach(e -> {
94  editables.get(e).delete();
95  editables.remove(e);
96  });
97  }
98 
99  /* Object */
100 
101  @Override
102  public int hashCode() {
103  int hash = 5;
104  hash = 37 * hash + Objects.hashCode(this.resource);
105  return hash;
106  }
107 
108  @Override
109  public boolean equals(Object obj) {
110  if (this == obj) {
111  return true;
112  }
113  if (obj == null) {
114  return false;
115  }
116  if (getClass() != obj.getClass()) {
117  return false;
118  }
119  final WebResource other = (WebResource) obj;
120  return Objects.equals(this.resource, other.resource);
121  }
122 
123 }
void addEditable(String modified, File file)
static WebResourceParts decompose(File file)
void addFile(WebResourceParts wrp, File file)