Project

General

Profile

1
package eu.dnetlib.data.mdstore.model;
2

    
3
import java.time.Duration;
4
import java.time.LocalDate;
5
import java.time.ZoneId;
6
import java.util.Date;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.stream.Collectors;
10

    
11
import com.google.common.collect.Lists;
12
import com.google.common.collect.Maps;
13
import org.springframework.data.mongodb.core.index.CompoundIndex;
14
import org.springframework.data.mongodb.core.index.CompoundIndexes;
15
import org.springframework.data.mongodb.core.index.Indexed;
16
import org.springframework.data.mongodb.core.mapping.Document;
17

    
18
@Document
19
@CompoundIndexes({
20
		@CompoundIndex(name = "fli_idx", def = "{'format': 1, 'layout': 1, 'interpretation': 1}")
21
})
22
public class Metadata {
23

    
24
	@Indexed(unique = true)
25
	private String mdId;
26

    
27
	@Indexed
28
	private String currentId;
29

    
30
	private String format;
31

    
32
	private String layout;
33

    
34
	private String interpretation;
35

    
36
	private long size;
37

    
38
	private List<ReadLock> readLocks;
39

    
40
	private List<Transaction> transactions;
41

    
42
	public Metadata() {}
43

    
44
	public static Metadata create() {
45
		return new Metadata()
46
				.setSize(0)
47
				.setReadLocks(Lists.newArrayList())
48
				.setTransactions(Lists.newArrayList());
49
	}
50

    
51
	public List<String> getExpired(final int retainDays) {
52
		return getReadLocks().stream()
53
				.filter(r -> !r.getId().equals(getCurrentId()))
54
				.filter(r -> getExpiringDays(r) >= retainDays)
55
				.map(r -> r.getId())
56
				.collect(Collectors.toList());
57
	}
58

    
59
	public Map<String, Transaction> getTransactionMap() {
60
		final Map<String, Transaction> txs = Maps.newHashMap();
61
		getTransactions().forEach(t -> txs.put(t.getId(), t));
62
		return txs;
63
	}
64

    
65
	/**
66
	 * Gets the expiring days.
67
	 *
68
	 * @param info     the value
69
	 * @return the expiring days
70
	 */
71
	private long getExpiringDays(final ReadLock info) {
72
		final Long lastRead = info.getLastUpdate();
73
		final LocalDate readDate = new Date(lastRead).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
74
		return Duration.between(LocalDate.now().atTime(0, 0), readDate.atTime(0, 0)).toDays();
75
	}
76

    
77
	public String getMdId() {
78
		return mdId;
79
	}
80

    
81
	public String getFormat() {
82
		return format;
83
	}
84

    
85
	public String getLayout() {
86
		return layout;
87
	}
88

    
89
	public String getInterpretation() {
90
		return interpretation;
91
	}
92

    
93
	public long getSize() {
94
		return size;
95
	}
96

    
97
	public Metadata setMdId(final String mdId) {
98
		this.mdId = mdId;
99
		return this;
100
	}
101

    
102
	public Metadata setFormat(final String format) {
103
		this.format = format;
104
		return this;
105
	}
106

    
107
	public Metadata setLayout(final String layout) {
108
		this.layout = layout;
109
		return this;
110
	}
111

    
112
	public Metadata setInterpretation(final String interpretation) {
113
		this.interpretation = interpretation;
114
		return this;
115
	}
116

    
117
	public Metadata setSize(final long size) {
118
		this.size = size;
119
		return this;
120
	}
121

    
122
	public String getCurrentId() {
123
		return this.currentId;
124
	}
125

    
126
	public List<ReadLock> getReadLocks() {
127
		return this.readLocks;
128
	}
129

    
130
	public List<Transaction> getTransactions() {
131
		return this.transactions;
132
	}
133

    
134
	public Metadata setCurrentId(final String currentId) {
135
		this.currentId = currentId;
136
		return this;
137
	}
138

    
139
	public Metadata setReadLocks(final List<ReadLock> readLocks) {
140
		this.readLocks = readLocks;
141
		return this;
142
	}
143

    
144
	public Metadata setTransactions(final List<Transaction> transactions) {
145
		this.transactions = transactions;
146
		return this;
147
	}
148

    
149
}
(3-3/5)