forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelDiff.java
More file actions
132 lines (115 loc) · 4.71 KB
/
ModelDiff.java
File metadata and controls
132 lines (115 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
*/
package com.gooddata.model;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.annotate.JsonTypeName;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
/**
* Model diff including MAQL DDL.
* Deserialization only.
*/
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("projectModelDiff")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ModelDiff {
private final List<UpdateScript> updateScripts;
/**
* @param updateScripts several variants of MAQL DDL evolution scripts that should be executed to update
* the source model to the target state
*/
@JsonCreator
ModelDiff(@JsonProperty("updateScripts") List<UpdateScript> updateScripts) {
this.updateScripts = updateScripts == null ? Collections.<UpdateScript>emptyList() : updateScripts;
}
/**
* Returns several variants of MAQL DDL evolution scripts that should be executed to update the source model to the
* target state. Individual variants differ in their side-effects (truncation of loaded data, drops of related objects...)
* <p/>
* Will be empty for empty diff.
*/
List<UpdateScript> getUpdateScripts() {
return unmodifiableList(updateScripts);
}
/**
* Returns MAQL DDL update script that should be executed to update the source model to the
* target state.
* <p/>
* It picks the best possible variant by it's side-effects (truncation
* of loaded data, drops of related objects). It returns first present variant by order:
* <ol>
* <li>preserve data + no cascade drops</li>
* <li>no preserve data + no cascade drops</li>
* <li>preserve data + cascade drops</li>
* <li>no preserve data + cascade drops</li>
* </ol>
* <p/>
* Returned list will be empty if there are no differences.
*
* @return MAQL DDL update scripts. Empty list if there are no differences.
*/
public List<String> getUpdateMaql() {
if (updateScripts.isEmpty()) {
return emptyList();
}
UpdateScript chosenScript;
if ((chosenScript = getUpdateScriptByFlags(true, false)) != null) {
return unmodifiableList(chosenScript.getMaqlChunks());
}
if ((chosenScript = getUpdateScriptByFlags(false, false)) != null) {
return unmodifiableList(chosenScript.getMaqlChunks());
}
if ((chosenScript = getUpdateScriptByFlags(true, true)) != null) {
return unmodifiableList(chosenScript.getMaqlChunks());
}
return unmodifiableList(updateScripts.get(0).getMaqlChunks());
}
/**
* Returns MAQL DDL update script by given flags determining side-effects (truncation of loaded data,
* drops of related objects).
*
* @param preserveData true to preserve data, false to truncate
* @param cascadeDrops true to drop all related objects
* @return update script with requested side-effect flags
*/
public UpdateScript getUpdateScriptByFlags(final boolean preserveData, final boolean cascadeDrops) {
UpdateScript result = null;
for (final UpdateScript script : updateScripts) {
if (script.isPreserveData() == preserveData && script.isCascadeDrops() == cascadeDrops) {
result = script;
}
}
return result;
}
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("updateScript")
@JsonIgnoreProperties(ignoreUnknown = true)
static class UpdateScript {
private List<String> maqlChunks;
private Boolean preserveData;
private Boolean cascadeDrops;
@JsonCreator
UpdateScript(@JsonProperty("maqlDdlChunks") List<String> maqlChunks,
@JsonProperty("preserveData") Boolean preserveData,
@JsonProperty("cascadeDrops") Boolean cascadeDrops) {
this.maqlChunks = maqlChunks == null ? Collections.<String>emptyList() : maqlChunks;
this.preserveData = preserveData;
this.cascadeDrops = cascadeDrops;
}
public List<String> getMaqlChunks() {
return maqlChunks;
}
public Boolean isPreserveData() {
return preserveData;
}
public Boolean isCascadeDrops() {
return cascadeDrops;
}
}
}