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 package info.magnolia.templating.models.components;
35
36 import info.magnolia.dam.api.Asset;
37 import info.magnolia.dam.api.AssetRendition;
38 import info.magnolia.dam.templating.functions.DamTemplatingFunctions;
39 import info.magnolia.jcr.util.NodeTypes;
40 import info.magnolia.jcr.util.NodeUtil;
41 import info.magnolia.jcr.util.PropertyUtil;
42 import info.magnolia.rendering.model.RenderingModel;
43 import info.magnolia.rendering.template.RenderableDefinition;
44 import info.magnolia.repository.RepositoryConstants;
45 import info.magnolia.templating.functions.TemplatingFunctions;
46
47 import javax.inject.Inject;
48 import javax.jcr.Node;
49 import javax.jcr.RepositoryException;
50
51 import org.apache.commons.lang3.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59
60 public class TeaserInternalModel<RD extends RenderableDefinition> extends AbstractTeaserModel {
61
62 private static final Logger log = LoggerFactory.getLogger(TeaserInternalModel.class);
63
64 protected final static String PROPERTY_NAME_PAGE_TITLE = "title";
65 protected final static String PROPERTY_NAME_PAGE_ABSTRACT = "abstract";
66
67 @Inject
68 public TeaserInternalModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions, DamTemplatingFunctions damTemplatingFunctions) {
69 super(content, definition, parent, templatingFunctions, damTemplatingFunctions);
70 }
71
72
73
74
75 protected Node getTarget() {
76 final String targetNodeIdentifier = PropertyUtil.getString(content, PROPERTY_NAME_LINK);
77
78 if (targetNodeIdentifier != null) {
79 final String path = NodeUtil.getPathIfPossible(content);
80 try {
81 final Node target = templatingFunctions.nodeById(targetNodeIdentifier, getRepository());
82 if (target != null) {
83 if (target.isNodeType(NodeTypes.Deleted.NAME)) {
84 return null;
85 }
86
87 final Node targetWrappedEncoded = templatingFunctions.encode(templatingFunctions.wrapForI18n(target));
88
89 return targetWrappedEncoded;
90 } else {
91 log.warn("Could not retrieve item for source node '{}' referenced by '{}' property", path, PROPERTY_NAME_LINK);
92 }
93 } catch (RepositoryException e) {
94 log.warn("Could not retrieve item for source node '{}' referenced by '{}' property", path, PROPERTY_NAME_LINK, e);
95 }
96 }
97
98 return null;
99 }
100
101
102
103
104
105 @Override
106 public AssetRendition getImage() {
107 final AssetRendition rendition = super.getImage();
108 if (rendition != null) {
109 return rendition;
110 }
111
112 final String targetNodeIdentifier = PropertyUtil.getString(content, PROPERTY_NAME_LINK);
113 if (targetNodeIdentifier != null) {
114 final Node target = templatingFunctions.nodeById(targetNodeIdentifier, getRepository());
115 if (target != null) {
116 final Asset image = findImage(target);
117 if (image != null) {
118 return damTemplatingFunctions.getRendition(image, getImageVariationName());
119 }
120 }
121 }
122
123 return null;
124 }
125
126
127
128
129
130 protected Asset findImage(Node target) {
131 if (target != null) {
132 final String assetIdentifier = PropertyUtil.getString(target, getImageName());
133 if (StringUtils.isNotBlank(assetIdentifier)) {
134 return damTemplatingFunctions.getAsset(assetIdentifier);
135 }
136
137 try {
138 for (final Node child : NodeUtil.getNodes(target)) {
139 return findImage(child);
140 }
141 } catch (RepositoryException e) {
142 log.error("Could not get nodes for target node [{}]", target, e);
143 }
144 }
145
146 return null;
147 }
148
149
150
151
152
153
154 @Override
155 public String getTeaserLink() {
156 final String targetNodeIdentifier = PropertyUtil.getString(content, PROPERTY_NAME_LINK);
157 return templatingFunctions.link(getRepository(), targetNodeIdentifier);
158 }
159
160
161
162
163 @Override
164 public String getTeaserTitle() {
165 String teaserTitle = super.getTeaserTitle();
166
167 if (StringUtils.isBlank(teaserTitle)) {
168 final Node target = getTarget();
169 if (target != null) {
170 teaserTitle = PropertyUtil.getString(target, PROPERTY_NAME_PAGE_TITLE);
171 if (StringUtils.isBlank(teaserTitle)) {
172 teaserTitle = NodeUtil.getName(target);
173 }
174 }
175 }
176
177 return teaserTitle;
178 }
179
180
181
182
183 @Override
184 public String getTeaserText() {
185 String teaserText = super.getTeaserText();
186
187 if (StringUtils.isBlank(teaserText)) {
188 final Node target = getTarget();
189 if (target != null) {
190 teaserText = PropertyUtil.getString(target, PROPERTY_NAME_PAGE_ABSTRACT);
191 }
192 }
193
194 return teaserText;
195 }
196
197
198
199
200 @Override
201 public boolean isTargetBlank() {
202 return false;
203 }
204
205
206
207
208 protected static String getRepository() {
209 return RepositoryConstants.WEBSITE;
210 }
211
212 }