/
BrokerComponentProvider.java
141 lines (110 loc) · 4.71 KB
/
BrokerComponentProvider.java
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
133
134
135
136
137
138
139
140
141
package org.dd4t.providers.impl;
import java.text.ParseException;
import org.dd4t.contentmodel.exceptions.ItemNotFoundException;
import org.dd4t.providers.ComponentProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.tridion.broker.StorageException;
import com.tridion.dcp.ComponentPresentation;
import com.tridion.dcp.ComponentPresentationFactory;
import com.tridion.storage.ComponentMeta;
import com.tridion.storage.StorageManagerFactory;
import com.tridion.storage.StorageTypeMapping;
import com.tridion.storage.dao.ItemDAO;
import com.tridion.util.TCMURI;
public class BrokerComponentProvider implements ComponentProvider {
protected static Logger logger = LoggerFactory.getLogger(BrokerComponentProvider.class);
// default CT to use when finding CP
private String defaultComponentTemplateUri;
// default output to use when finding CP
private String defaultOutputFormat;
public String getDefaultOutputFormat() {
return defaultOutputFormat;
}
public void setDefaultOutputFormat(String defaultOutputFormat) {
this.defaultOutputFormat = defaultOutputFormat;
}
public String getDefaultComponentTemplateUri() {
return defaultComponentTemplateUri;
}
public void setDefaultComponentTemplateUri(String defaultComponentTemplateUri) {
this.defaultComponentTemplateUri = defaultComponentTemplateUri;
}
@Override
public String getComponentXML(int componentId, int publicationId) throws StorageException, ItemNotFoundException{
return getComponentXMLByTemplate(componentId, 0, publicationId);
}
@Override
public String getComponentXMLByTemplate(int componentId, int templateId,
int publicationId) throws StorageException, ItemNotFoundException{
ComponentPresentation cp = getDynamicComponentPresentation(componentId, templateId, publicationId);
return cp.getContent();
}
@Override
public ComponentMeta getComponentMeta(int componentId, int publicationId)
throws StorageException {
ItemDAO itemDAO = (ItemDAO) StorageManagerFactory.getDAO(publicationId, StorageTypeMapping.COMPONENT_META);
return (ComponentMeta) itemDAO.findByPrimaryKey(publicationId, componentId);
}
@Override
public ComponentPresentation getDynamicComponentPresentation(
int componentId, int publicationId) throws StorageException, ItemNotFoundException{
return getDynamicComponentPresentation(componentId, 0, publicationId);
}
@Override
public ComponentPresentation getDynamicComponentPresentation(
int componentId, int templateId, int publicationId)
throws StorageException, ItemNotFoundException {
ComponentPresentationFactory factory = new ComponentPresentationFactory(publicationId);
ComponentPresentation cp = null;
if (templateId != 0) {
logger.debug("templateId requested");
cp = factory.getComponentPresentation(
componentId, templateId);
if (cp == null) {
logger.debug("component presentation NOT found by template");
// no cp found, and they asked for a template, means we have to throw an exception
throw new ItemNotFoundException("Unable to find DCP by template for pub "+publicationId+", item "+componentId+" and template "+templateId);
}
logger.debug("component presentation found by template. ");
return cp;
}
// option 1: use defaultComponentTemplateUri (if given)
String defCT = this.getDefaultComponentTemplateUri();
if (defCT != null && !defCT.equals("")) {
logger.debug("defaultComponentTemplateUri is specified as "
+ defCT);
TCMURI defCTUri;
try {
defCTUri = new TCMURI(this.defaultComponentTemplateUri);
cp = factory.getComponentPresentation(componentId, defCTUri.getItemId());
} catch (ParseException e) {
logger.warn(
"malformed defaultComponentTemplateUri in configuration",
e);
}
}
// option 2: use outputFormat (if given)
if (cp == null) {
// still no cp, try option 2:
// use OutputFormat
String defOF = this.getDefaultOutputFormat();
if (defOF != null && !defOF.equals("")) {
logger.debug("defaultOutputFormat is specified as " + defOF);
cp = factory.getComponentPresentationWithOutputFormat(
componentId, defOF);
}
}
// option 3: nothing specified, just use priority
if(cp == null){
logger.debug("attempting to find component presentation on priority");
// then lets check priority
cp = factory.getComponentPresentationWithHighestPriority(componentId);
}
if (cp != null) {
logger.debug("component presentation found");
return cp;
}
throw new ItemNotFoundException("Unable to find DCP for pub "+publicationId+", item "+componentId+" and template "+templateId);
}
}