55import com .sdl .webapp .common .api .content .LinkResolver ;
66import com .sdl .webapp .common .api .localization .Localization ;
77import com .sdl .webapp .common .api .mapping .semantic .SemanticMappingException ;
8+ import com .sdl .webapp .common .api .model .EntityModel ;
89import com .sdl .webapp .common .api .model .entity .AbstractEntityModel ;
910import com .sdl .webapp .common .api .model .entity .Link ;
11+ import com .sdl .webapp .common .util .TcmUtils ;
1012import com .sdl .webapp .tridion .fields .exceptions .FieldConverterException ;
1113import com .sdl .webapp .tridion .fields .exceptions .UnsupportedTargetTypeException ;
1214import com .sdl .webapp .tridion .mapping .ModelBuilderPipeline ;
@@ -26,6 +28,7 @@ public class ComponentLinkFieldConverter extends AbstractFieldConverter {
2628 private static final FieldType [] SUPPORTED_FIELD_TYPES = {FieldType .COMPONENTLINK };
2729
2830 private final LinkResolver linkResolver ;
31+
2932 private final WebRequestContext webRequestContext ;
3033
3134 @ Autowired
@@ -69,10 +72,11 @@ public Object createPageLink(org.dd4t.contentmodel.Page page, Class<?> targetCla
6972 String pageId = page .getId ();
7073 final String url = linkResolver .resolveLink (pageId , null );
7174
72- if (targetClass . isAssignableFrom ( String .class ) ) {
75+ if (targetClass == String .class ) {
7376 return url ;
74- } else if (targetClass . isAssignableFrom ( Link .class )) {
77+ } else if (Link .class . isAssignableFrom ( targetClass )) {
7578 final Link link = new Link ();
79+ link .setId (String .valueOf (TcmUtils .getItemId (pageId )));
7680 link .setUrl (url );
7781 return link ;
7882 } else {
@@ -85,18 +89,20 @@ public Object createComponentLink(org.dd4t.contentmodel.Component component, Cla
8589 String componentId = component .getId ();
8690 final String url = linkResolver .resolveLink (componentId , null );
8791
88- if (targetClass . isAssignableFrom ( String .class ) ) {
92+ if (String .class == targetClass ) {
8993 return url ;
90- } else if (targetClass . isAssignableFrom ( Link .class )) {
94+ } else if (Link .class . isAssignableFrom ( targetClass )) {
9195 final Link link = new Link ();
96+ link .setId (String .valueOf (TcmUtils .getItemId (componentId )));
9297 link .setUrl (url );
9398 return link ;
94- } else if (AbstractEntityModel .class .isAssignableFrom (targetClass )) {
99+ } else if (EntityModel .class .isAssignableFrom (targetClass )) { //what we want should be assignable from what we want to have
95100 Localization localization = this .webRequestContext .getLocalization ();
96101
97102 try {
98103 Object retval = builder .createEntityModel (component , localization );
99- if (targetClass .isAssignableFrom (retval .getClass ())) {
104+ // what we got should be assignable from what we expected
105+ if (retval != null && targetClass .isAssignableFrom (retval .getClass ())) {
100106 return retval ;
101107 } else {
102108 return null ;
0 commit comments