1 | package genius.core.repository;
|
---|
2 |
|
---|
3 | import java.net.URL;
|
---|
4 |
|
---|
5 | import javax.xml.bind.Unmarshaller;
|
---|
6 | import javax.xml.bind.annotation.XmlAttribute;
|
---|
7 | import javax.xml.bind.annotation.XmlRootElement;
|
---|
8 | import javax.xml.bind.annotation.XmlTransient;
|
---|
9 |
|
---|
10 | import genius.core.Domain;
|
---|
11 | import genius.core.exceptions.Warning;
|
---|
12 | import genius.core.session.RepositoryException;
|
---|
13 | import genius.core.utility.UtilitySpace;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * ProfileRepItem is a profile, as an item to put in the registry. The profile
|
---|
17 | * is not contained here, it's just a (assumed unique) filename. immutable.
|
---|
18 | *
|
---|
19 | * @author W.Pasman added code to unmarshall this if not part of a
|
---|
20 | * domainrepository.xml file. We then search for this profile in the
|
---|
21 | * existing domain repository. see
|
---|
22 | * {@link #afterUnmarshal(Unmarshaller, Object)}.
|
---|
23 | *
|
---|
24 | */
|
---|
25 | @XmlRootElement
|
---|
26 | public class ProfileRepItem implements RepItem {
|
---|
27 | private static final long serialVersionUID = -5071749178482314158L;
|
---|
28 | @XmlAttribute
|
---|
29 | private URL url;
|
---|
30 |
|
---|
31 | @XmlTransient
|
---|
32 | private DomainRepItem domain;
|
---|
33 |
|
---|
34 | /** needed by XML serializer */
|
---|
35 | @SuppressWarnings("unused")
|
---|
36 | protected ProfileRepItem() {
|
---|
37 | try {
|
---|
38 | url = new URL("file:xml-failed-to-set-url");
|
---|
39 | } catch (Exception e) {
|
---|
40 | new Warning("failed to set filename default value" + e);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public ProfileRepItem(URL file, DomainRepItem dom) {
|
---|
45 | url = file;
|
---|
46 | domain = dom;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public URL getURL() {
|
---|
50 | return url;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public DomainRepItem getDomain() {
|
---|
54 | return domain;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public String toString() {
|
---|
59 | return getURL().getFile();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @return a full name but without any special characters like "/" and ":".
|
---|
64 | */
|
---|
65 | public String getFullName() {
|
---|
66 | return url.toString().replaceAll("/", ".").replaceAll(":", ".");
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * See {@link Unmarshaller}.
|
---|
71 | *
|
---|
72 | * @param u
|
---|
73 | * @param parent
|
---|
74 | */
|
---|
75 | public void afterUnmarshal(Unmarshaller u, Object parent) {
|
---|
76 | if (parent instanceof DomainRepItem) {
|
---|
77 | domain = (DomainRepItem) parent;
|
---|
78 | } else {
|
---|
79 | domain = searchDomain();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Try to find this profile in the domain repository. This is needed if this
|
---|
85 | * profilerepitem is not part of a domainrepository.xml file.
|
---|
86 | *
|
---|
87 | * @return DomainRepItem. Returns null if this profile is part of an
|
---|
88 | * existing domain in the repository
|
---|
89 | */
|
---|
90 | private DomainRepItem searchDomain() {
|
---|
91 | // this ProfileRepItem is not in a domain repository. Try to resolve
|
---|
92 | // domain using the repository
|
---|
93 | try {
|
---|
94 | for (RepItem item : RepositoryFactory.get_domain_repos().getItems()) {
|
---|
95 | DomainRepItem repitem = (DomainRepItem) item;
|
---|
96 | if (repitem.getProfiles().contains(this)) {
|
---|
97 | return repitem;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | } catch (RepositoryException e) {
|
---|
101 | e.printStackTrace();
|
---|
102 | }
|
---|
103 | System.err.println("The profile " + this + " is not in the domain repository, failed to unmarshall");
|
---|
104 | throw new NullPointerException("no domain found");
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public int hashCode() {
|
---|
109 | int hash = 7;
|
---|
110 | hash = 97 * hash + (this.url != null ? this.url.hashCode() : 0);
|
---|
111 | return hash;
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public boolean equals(Object o) {
|
---|
116 | if (!(o instanceof ProfileRepItem)) {
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 | return url.equals(((ProfileRepItem) o).getURL());
|
---|
120 | }
|
---|
121 |
|
---|
122 | public String getName() {
|
---|
123 | String name = url.getFile();
|
---|
124 | if (name.contains("/") && name.contains(".")) {
|
---|
125 | name = name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf("."));
|
---|
126 | }
|
---|
127 | return name;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | *
|
---|
132 | * @return a new UtilitySpace referred to by this ProfileRepItem. If
|
---|
133 | * {@link ProfileRepItem#getDomain()} returns new instead of an
|
---|
134 | * actual domain, this method also returns null.
|
---|
135 | * @throws RepositoryException
|
---|
136 | */
|
---|
137 | public UtilitySpace create() throws RepositoryException {
|
---|
138 | Domain domain;
|
---|
139 | try {
|
---|
140 | domain = RepositoryFactory.get_domain_repos().getDomain(getDomain());
|
---|
141 |
|
---|
142 | return RepositoryFactory.get_domain_repos().getUtilitySpace(domain, this);
|
---|
143 | } catch (Exception e) {
|
---|
144 | throw new RepositoryException("File not found for " + this, e);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | }
|
---|