Skip to content

Commit d1a7328

Browse files
author
Libor Rysavy
committed
Merge pull request #33 from martiner/rep
Add MetadataService.getObj() method returning Obj instance by restrictions
2 parents f80fcbd + 9b4da7f commit d1a7328

File tree

16 files changed

+179
-100
lines changed

16 files changed

+179
-100
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ Query, create and update project metadata - attributes, facts, metrics, reports,
5252
```java
5353
MetadataService md = gd.getMetadataService();
5454

55-
String factUri = md.findObjUri(project, Fact.class, Restriction.title("myfact"));
55+
String factUri = md.getObjUri(project, Fact.class, Restriction.title("myfact"));
56+
Metric m = new Metric("my sum", "SELECT SUM([" + factUri + "])", "#,##0");
57+
Metric metric = md.createObj(project, m);
58+
Attribute attr = md.getObj(project, Attribute.class, Restriction.title("myattr"));
5659

5760
Metric m = md.createObj(project, new Metric("My Sum", "SELECT SUM([" + factUri + "])", "#,##0"));
5861

src/main/java/com/gooddata/Validate.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ public static <T extends Collection> T noNullElements(T collection, String argum
5959
return collection;
6060
}
6161

62+
public static <T> T[] noNullElements(T[] array, String argument) {
63+
notNull(array, argument);
64+
for (int i = 0; i<array.length; i++) {
65+
if (array[i] == null) {
66+
throw new IllegalArgumentException(argument + " contains null element at index: " + i);
67+
}
68+
}
69+
return array;
70+
}
71+
6272
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.md;
5+
6+
import org.codehaus.jackson.annotate.JsonIgnore;
7+
import org.codehaus.jackson.annotate.JsonProperty;
8+
9+
import static com.gooddata.Validate.noNullElements;
10+
11+
/**
12+
* Metadata object (common part)
13+
*/
14+
public abstract class AbstractObj implements Obj {
15+
16+
@JsonProperty("meta")
17+
protected final Meta meta;
18+
19+
protected AbstractObj(@JsonProperty("meta") Meta meta) {
20+
this.meta = meta;
21+
}
22+
23+
@JsonIgnore
24+
public String getAuthor() {
25+
return meta.getAuthor();
26+
}
27+
28+
@JsonIgnore
29+
public String getContributor() {
30+
return meta.getContributor();
31+
}
32+
33+
@JsonIgnore
34+
public String getCreated() {
35+
return meta.getCreated();
36+
}
37+
38+
@JsonIgnore
39+
public String getSummary() {
40+
return meta.getSummary();
41+
}
42+
43+
@JsonIgnore
44+
public String getTitle() {
45+
return meta.getTitle();
46+
}
47+
48+
@JsonIgnore
49+
public String getUpdated() {
50+
return meta.getUpdated();
51+
}
52+
53+
@JsonIgnore
54+
public String getCategory() {
55+
return meta.getCategory();
56+
}
57+
58+
@JsonIgnore
59+
public String getTags() {
60+
return meta.getTags();
61+
}
62+
63+
@JsonIgnore
64+
public String getUri() {
65+
return meta.getUri();
66+
}
67+
68+
@JsonIgnore
69+
public String getDeprecated() {
70+
return meta.getDeprecated();
71+
}
72+
73+
@JsonIgnore
74+
public String getIdentifier() {
75+
return meta.getIdentifier();
76+
}
77+
78+
@JsonIgnore
79+
public Integer getLocked() {
80+
return meta.getLocked();
81+
}
82+
83+
@JsonIgnore
84+
public Integer getUnlisted() {
85+
return meta.getUnlisted();
86+
}
87+
88+
/**
89+
* Get list of URIs of the given {@link Obj}s
90+
* @param objs metadata objects
91+
* @return list of URIs
92+
*/
93+
@SafeVarargs
94+
protected static <T extends Obj> String[] uris(T... objs) {
95+
noNullElements(objs, "objs");
96+
final String[] uris = new String[objs.length];
97+
for (int i=0; i<objs.length; i++) {
98+
uris[i] = objs[i].getUri();
99+
}
100+
return uris;
101+
}
102+
103+
}

src/main/java/com/gooddata/md/Attribute.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
2424
@JsonIgnoreProperties(ignoreUnknown = true)
2525
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
26-
public class Attribute extends Obj implements Queryable {
26+
public class Attribute extends AbstractObj implements Queryable {
2727

2828
@JsonProperty("content")
2929
private final Content content;
@@ -55,6 +55,11 @@ public Collection<Key> getForeignKeys() {
5555
return content.getFk();
5656
}
5757

58+
@JsonIgnore
59+
public DisplayForm getDefaultDisplayForm() {
60+
return getDisplayForms().iterator().next();
61+
}
62+
5863
@JsonIgnoreProperties(ignoreUnknown = true)
5964
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
6065
private static class Content {

src/main/java/com/gooddata/md/DisplayForm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
@JsonIgnoreProperties(ignoreUnknown = true)
1616
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
17-
public class DisplayForm extends Obj {
17+
public class DisplayForm extends AbstractObj {
1818

1919
@JsonProperty("content")
2020
private final Content content;

src/main/java/com/gooddata/md/Fact.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
2323
@JsonIgnoreProperties(ignoreUnknown = true)
2424
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
25-
public class Fact extends Obj implements Queryable {
25+
public class Fact extends AbstractObj implements Queryable {
2626

2727
@JsonProperty("content")
2828
private final Content content;

src/main/java/com/gooddata/md/MetadataService.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public <T extends Obj> T getObjById(Project project, String id, Class<T> cls) {
121121
* @throws com.gooddata.md.ObjNotFoundException if metadata object not found
122122
* @throws com.gooddata.md.NonUniqueObjException if more than one object corresponds to search restrictions
123123
*/
124-
public <T extends Queryable> String findObjUri(Project project, Class<T> cls, Restriction... restrictions) {
124+
public <T extends Queryable> String getObjUri(Project project, Class<T> cls, Restriction... restrictions) {
125125
final Collection<String> results = findUris(project, cls, restrictions);
126126
if (results == null || results.isEmpty()) {
127127
throw new ObjNotFoundException(cls);
@@ -131,6 +131,27 @@ public <T extends Queryable> String findObjUri(Project project, Class<T> cls, Re
131131
return results.iterator().next();
132132
}
133133

134+
/**
135+
* Get metadata object by restrictions like identifier, title or summary.
136+
*
137+
* @param project project where to search for the object
138+
* @param cls class of the resulting object
139+
* @param restrictions query restrictions
140+
* @param <T> type of the object to be returned
141+
* @return metadata object
142+
* @throws com.gooddata.md.ObjNotFoundException if metadata object not found
143+
* @throws com.gooddata.md.NonUniqueObjException if more than one object corresponds to search restrictions
144+
*/
145+
public <T extends Queryable> T getObj(Project project, Class<T> cls, Restriction... restrictions) {
146+
final Collection<String> results = findUris(project, cls, restrictions);
147+
if (results == null || results.isEmpty()) {
148+
throw new ObjNotFoundException(cls);
149+
} else if (results.size() != 1) {
150+
throw new NonUniqueObjException(cls, results);
151+
}
152+
return getObjByUri(results.iterator().next(), cls);
153+
}
154+
134155
/**
135156
* Find metadata by restrictions like identifier, title or summary.
136157
*

src/main/java/com/gooddata/md/Metric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
1919
@JsonIgnoreProperties(ignoreUnknown = true)
2020
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
21-
public class Metric extends Obj implements Queryable {
21+
public class Metric extends AbstractObj implements Queryable {
2222

2323
@JsonProperty("content")
2424
private final Content content;
Lines changed: 6 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,15 @@
1-
/*
2-
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3-
*/
41
package com.gooddata.md;
52

6-
import org.codehaus.jackson.annotate.JsonIgnore;
7-
import org.codehaus.jackson.annotate.JsonProperty;
83
import org.springframework.web.util.UriTemplate;
94

105
/**
11-
* Metadata object (common part)
6+
* Metadata object
127
*/
13-
public abstract class Obj {
14-
public static final String URI = "/gdc/md/{projectId}/obj";
15-
public static final String OBJ_URI = URI + "/{objId}";
16-
public static final UriTemplate OBJ_TEMPLATE = new UriTemplate(OBJ_URI);
17-
18-
@JsonProperty("meta")
19-
protected final Meta meta;
20-
21-
protected Obj(@JsonProperty("meta") Meta meta) {
22-
this.meta = meta;
23-
}
24-
25-
@JsonIgnore
26-
public String getAuthor() {
27-
return meta.getAuthor();
28-
}
29-
30-
@JsonIgnore
31-
public String getContributor() {
32-
return meta.getContributor();
33-
}
34-
35-
@JsonIgnore
36-
public String getCreated() {
37-
return meta.getCreated();
38-
}
39-
40-
@JsonIgnore
41-
public String getSummary() {
42-
return meta.getSummary();
43-
}
44-
45-
@JsonIgnore
46-
public String getTitle() {
47-
return meta.getTitle();
48-
}
49-
50-
@JsonIgnore
51-
public String getUpdated() {
52-
return meta.getUpdated();
53-
}
54-
55-
@JsonIgnore
56-
public String getCategory() {
57-
return meta.getCategory();
58-
}
59-
60-
@JsonIgnore
61-
public String getTags() {
62-
return meta.getTags();
63-
}
64-
65-
@JsonIgnore
66-
public String getUri() {
67-
return meta.getUri();
68-
}
69-
70-
@JsonIgnore
71-
public String getDeprecated() {
72-
return meta.getDeprecated();
73-
}
74-
75-
@JsonIgnore
76-
public String getIdentifier() {
77-
return meta.getIdentifier();
78-
}
79-
80-
@JsonIgnore
81-
public Integer getLocked() {
82-
return meta.getLocked();
83-
}
8+
public interface Obj {
849

85-
@JsonIgnore
86-
public Integer getUnlisted() {
87-
return meta.getUnlisted();
88-
}
10+
String URI = "/gdc/md/{projectId}/obj";
11+
String OBJ_URI = URI + "/{objId}";
12+
UriTemplate OBJ_TEMPLATE = new UriTemplate(OBJ_URI);
8913

14+
String getUri();
9015
}

src/main/java/com/gooddata/md/Queryable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
/**
44
* Marker interface for metadata objects which can be found using query resource (see MetadataService.find* methods).
55
*/
6-
public interface Queryable {
6+
public interface Queryable extends Obj {
77
}

0 commit comments

Comments
 (0)