Project

General

Profile

ObsoletesChain.java

refactored to track possible NotAuthorized situations - Rob Nahf, 2013-08-08 20:39

Download (4.47 KB)

 
1
package org.dataone.client.types;
2

    
3
import java.util.Date;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.LinkedList;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
import java.util.TreeMap;
11

    
12
import org.dataone.service.types.v1.Identifier;
13

    
14

    
15
/**
16
 * An object meant to contain information on the history of an object, including
17
 * all of the objects the given pid obsoletes, and objects that obsolete it.
18
 * @author rnahf
19
 *
20
 */
21
public class ObsoletesChain {
22
    
23
        private Identifier startingPid;
24
        private List<Object[]> infoTable;
25
        private TreeMap<Long,Integer> byDateIndex;
26
        private Map<Identifier,Integer> byIdIndex;
27
        
28
        private final static int PID = 0;
29
        private final static int PUBLISH_DATE = 1;
30
        private final static int OBSOLETES = 2;
31
        private final static int OBSOLETEDBY = 3;
32
        private final static int IS_ARCHIVED = 4;
33
        private final static int IS_NOT_AUTHORIZED = 5;
34
        
35
        public ObsoletesChain(Identifier pid) {
36
                this.startingPid = pid;
37
                this.infoTable = new LinkedList<Object[]>();
38
                this.byDateIndex = new TreeMap<Long, Integer>();
39
                this.byIdIndex = new HashMap<Identifier, Integer>();
40

    
41
        }
42
        
43
        public Identifier getStartingPoint() {
44
                return this.startingPid;
45
        }
46
        
47
        
48
        public void addObject(Identifier pid, Date publishDate, 
49
        Identifier obsoletes, Identifier obsoletedBy, Boolean isArchived, Boolean isNotAuthorized) {
50
                if( publishDate == null) {
51
                        throw new NullPointerException("publishDate parameter cannot be null.");
52
                }
53
                this.infoTable.add(new Object[]{pid, publishDate, obsoletes, obsoletedBy, isArchived, isNotAuthorized});
54
                this.byDateIndex.put(publishDate.getTime(), this.infoTable.size()-1);
55
                this.byIdIndex.put(pid, this.infoTable.size()-1);
56
        }
57
        
58
//        public void removeObject(Identifier pid) {
59
                // THIS WILL BE DIFFICULT TO IMPLEMENT, AND PROBABLY NOT USEFUL
60
                // EASIER TO START OVER, EH?
61
//                Integer index = this.byIdIndex.get(pid);
62
//                this.byDateMap.remove(publishDate);
63
//                this.byIdMap.remove(pid);
64
//        }
65
        
66
        public Identifier getVersionAsOf(Date asOfDate) {
67
                Long asOf = asOfDate.getTime();
68
                Iterator<Long> it = this.byDateIndex.keySet().iterator();
69
                Long time = null;
70
                while (it.hasNext()) {
71
                        Long nextTime = it.next();
72
                        if (asOf < nextTime) {
73
                                break;
74
                        }
75
                        time = nextTime;
76
                }
77
                if (time == null)
78
                        return null;
79
                
80
                int tableIndex = this.byDateIndex.get(time);
81
                return (Identifier) this.infoTable.get(tableIndex)[PID];
82
        }
83
        
84
        public Identifier nextVersion(Identifier pid) {
85
                int tableIndex = this.byIdIndex.get(pid);
86
                return (Identifier) this.infoTable.get(tableIndex)[OBSOLETEDBY];
87
                
88
        }
89
        
90
        public Identifier previousVersion(Identifier pid) {
91
                int tableIndex = this.byIdIndex.get(pid);
92
                return (Identifier) this.infoTable.get(tableIndex)[OBSOLETES];
93
        }
94
        
95

    
96
        public Identifier getLatestVersion() {
97
                // this seems quite inefficient...
98
                return getByPosition(size()-1);
99
        }
100
                
101
        public Identifier getOriginalVersion() {
102
                return getByPosition(0);
103
                
104
        }
105
        
106
        public Identifier getByPosition(int index) {
107
                if (index < 0 || index >= this.infoTable.size())
108
                        throw new IndexOutOfBoundsException("The provided index does not exist");
109
                
110
                Iterator<Long> it = this.byDateIndex.keySet().iterator();
111
                Long time = null;
112
                int i = -1;
113
                while (it.hasNext()) {
114
                        time = it.next();
115
                        i++;
116
                        if (index == i) {
117
                                break;
118
                        }
119
                }
120
                int tableIndex = this.byDateIndex.get(time);
121
                return (Identifier) this.infoTable.get(tableIndex)[PID];
122
        }
123
        
124
        /**
125
         * returns the size of the chain (count of the number of items)
126
         * @return
127
         */
128
        public int size() {
129
                return this.infoTable.size();
130
        }
131
        
132
        /**
133
         * An ObsoletesChain is complete when the earliest and latest links have
134
         * no obsoletedBy and obsoletes, respectively.
135
         */
136
        public boolean isComplete() {
137
                if (this.infoTable.get(this.byIdIndex.get(getOriginalVersion()))[OBSOLETES] == null &&
138
                        this.infoTable.get(this.byIdIndex.get(getLatestVersion()))[OBSOLETEDBY] == null) 
139
                {
140
                        return true;
141
                }
142
                return false;
143
        }
144
        
145
        public Boolean isArchived(Identifier pid) {
146
                int tableIndex = this.byIdIndex.get(pid);
147
                Object[] oa = this.infoTable.get(tableIndex);
148
                return oa[IS_ARCHIVED] == null ? Boolean.FALSE : (Boolean) oa[IS_ARCHIVED];                
149
        }
150
        
151
        public Boolean latestIsArchived() {
152
                return isArchived(getLatestVersion());
153
        }
154
        
155
        public Date getPublishDate(Identifier pid) {
156
                int tableIndex = this.byIdIndex.get(pid);
157
                return (Date) this.infoTable.get(tableIndex)[PUBLISH_DATE];
158
        }
159
        
160
        public boolean isLatestVersion(Identifier pid) {
161
                if (this.infoTable.get(this.byIdIndex.get(pid))[OBSOLETEDBY] == null) {
162
                        return true;
163
                }
164
                return false;
165
        }
166
}
Add picture from clipboard (Maximum size: 14.8 MB)