Skip to content

Commit

Permalink
- Code Smells: WEM and Search
Browse files Browse the repository at this point in the history
  • Loading branch information
alegauss committed Sep 29, 2024
1 parent 21da504 commit 8dafd01
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.viglet.turing.persistence.dto.sn.field.TurSNSiteFieldExtDto;
import com.viglet.turing.persistence.dto.sn.field.TurSNSiteFieldExtFacetDto;
import com.viglet.turing.persistence.model.sn.TurSNSite;
import com.viglet.turing.persistence.model.sn.field.TurSNSiteFieldExt;
import com.viglet.turing.persistence.model.sn.field.TurSNSiteFieldExtFacet;
import com.viglet.turing.persistence.model.sn.metric.TurSNSiteMetricAccess;
import com.viglet.turing.persistence.repository.sn.TurSNSiteRepository;
Expand Down
10 changes: 5 additions & 5 deletions turing-app/src/main/java/com/viglet/turing/solr/TurSolr.java
Original file line number Diff line number Diff line change
Expand Up @@ -891,11 +891,11 @@ private static boolean isFacetTypeDefault(TurSNSiteFacetFieldEnum turSNSiteFacet

private static TurSNSiteFacetFieldEnum getFacetType(TurSNSiteFieldExt facet, TurSNSite turSNSite,
TurSNFilterQueryOperator operator) {
return operatorIsNotEmpty(operator) ?
getFaceTypeFromOperator(operator) :
isFacetTypeDefault(facet.getFacetType()) ?
getFacetTypeFromSite(turSNSite) :
facet.getFacetType();
if (operatorIsNotEmpty(operator))
return getFaceTypeFromOperator(operator);
if (isFacetTypeDefault(facet.getFacetType()))
return getFacetTypeFromSite(turSNSite);
return facet.getFacetType();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* @since 0.3.9
*/
public class TurSNFieldName {
private TurSNFieldName() {
throw new IllegalStateException("Semantic Navigation Field Names class");
}
public static final String ID = "id";
public static final String URL = "url";
public static final String ABSTRACT = "abstract";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ private void runByGuidList()

private void runAllObjectTypes() throws ApplicationException, ConfigException, ValidationException {
IPagingList contentTypeIPagingList = ContentType.findAll();
@SuppressWarnings("unchecked")
List<Object> contentTypes = contentTypeIPagingList.asList();
contentTypes.add(StaticFile.getTypeObjectTypeRef().getObjectType());

Expand Down Expand Up @@ -203,7 +202,7 @@ private void indexByContentType(String siteName, ObjectType objectType)
rp.setTopRelationOnly(false);

AsObjectType aot = AsObjectType.getInstance(new ObjectTypeRef((ManagedObject) objectType));
IValidToIndex instance = mappingDefinitions.validToIndex(objectType, turingConfig);
IValidToIndex instance = mappingDefinitions.validToIndex(objectType);
if (aot.isStaticFile()) {
results = queryStaticFilesList(rp, instance);
} else {
Expand All @@ -227,8 +226,7 @@ private void indexByContentType(String siteName, ObjectType objectType)

}

private IPagingList queryContentInstanceList(ObjectType objectType, RequestParameters rp, IValidToIndex instance)
throws Exception {
private IPagingList queryContentInstanceList(ObjectType objectType, RequestParameters rp, IValidToIndex instance) {

IPagingList results;
ContentInstanceWhereClause clause = new ContentInstanceWhereClause();
Expand All @@ -241,7 +239,7 @@ private IPagingList queryContentInstanceList(ObjectType objectType, RequestParam
return results;
}

private IPagingList queryStaticFilesList(RequestParameters rp, IValidToIndex instance) throws Exception {
private IPagingList queryStaticFilesList(RequestParameters rp, IValidToIndex instance) {
IPagingList results;
StaticFileWhereClause clause = new StaticFileWhereClause();
StaticFileDBQuery query = new StaticFileDBQuery();
Expand Down Expand Up @@ -338,8 +336,7 @@ private void indexContentInstances(ManagedObjectVCMRef[] refs, HashMap<String, ?
ManagedObject mo = (ManagedObject) objects.get(ref.getId());
if (mo instanceof ContentInstance || mo instanceof Channel) {
if (log.isDebugEnabled())
log.debug(String.format("Attempting to index the Content Instance: %s",
mo.getContentManagementId().getId()));
log.debug("Attempting to index the Content Instance: {}", mo.getContentManagementId().getId());
TurWEMIndexer.indexCreate(mo, turingConfig, TuringUtils.getSiteNameFromContentInstance(mo, turingConfig));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
*/
package com.viglet.turing.connector.wem.broker.attribute;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;

import com.viglet.turing.commons.utils.TurCommonsUtils;
import com.viglet.turing.connector.wem.beans.TurAttrDef;
import com.viglet.turing.connector.wem.beans.TurAttrDefContext;
Expand All @@ -32,46 +28,54 @@
import com.vignette.as.client.javabean.ContentInstance;
import com.vignette.logging.context.ContextLogger;

public class TurWEMAttrClass {
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

private static final ContextLogger log = ContextLogger.getLogger(MethodHandles.lookup().lookupClass());
public static final String HTML = "html";
public class TurWEMAttrClass {

private TurWEMAttrClass() {
throw new IllegalStateException("TurWEMAttrClass");
}
private static final ContextLogger log = ContextLogger.getLogger(MethodHandles.lookup().lookupClass());
public static final String HTML = "html";

public static List<TurAttrDef> attributeByClass(TurAttrDefContext turAttrDefContext, AttributeData attributeData)
throws Exception {
private TurWEMAttrClass() {
throw new IllegalStateException("TurWEMAttrClass");
}

TuringTag turingTag = turAttrDefContext.getTuringTag();
ContentInstance ci = turAttrDefContext.getContentInstance();
IHandlerConfiguration config = turAttrDefContext.getiHandlerConfiguration();
List<TurAttrDef> attributesDefs = new ArrayList<>();
public static List<TurAttrDef> attributeByClass(TurAttrDefContext turAttrDefContext, AttributeData attributeData) {

if (turingTag.getSrcClassName() != null) {
String className = turingTag.getSrcClassName();
if (log.isDebugEnabled())
log.debug("ClassName : " + className);
TuringTag turingTag = turAttrDefContext.getTuringTag();
ContentInstance ci = turAttrDefContext.getContentInstance();
IHandlerConfiguration config = turAttrDefContext.getiHandlerConfiguration();
List<TurAttrDef> attributesDefs = new ArrayList<>();

Object extAttribute = Class.forName(className).getDeclaredConstructor().newInstance();
TurMultiValue turMultiValue = ((ExtAttributeInterface) extAttribute).consume(turingTag, ci, attributeData,
config);
TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue);
attributesDefs.add(turAttrDef);
} else {
TurMultiValue turMultiValue = new TurMultiValue();
if (turingTag.getSrcAttributeType() != null && turingTag.getSrcAttributeType().equals(HTML)) {
turMultiValue.add(TurCommonsUtils.html2Text(attributeData.getValue().toString()));
TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue);
attributesDefs.add(turAttrDef);
} else if (attributeData != null && attributeData.getValue() != null) {
turMultiValue.add(attributeData.getValue().toString());
TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue);
attributesDefs.add(turAttrDef);
}
if (turingTag.getSrcClassName() != null) {
String className = turingTag.getSrcClassName();
if (log.isDebugEnabled())
log.debug("ClassName : " + className);
try {
Object extAttribute = Class.forName(className).getDeclaredConstructor().newInstance();
TurMultiValue turMultiValue = ((ExtAttributeInterface) extAttribute).consume(turingTag, ci, attributeData,
config);
TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue);
attributesDefs.add(turAttrDef);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException | ClassNotFoundException e) {
log.error(e.getMessage(), e);
}
} else {
TurMultiValue turMultiValue = new TurMultiValue();
if (turingTag.getSrcAttributeType() != null && turingTag.getSrcAttributeType().equals(HTML)) {
turMultiValue.add(TurCommonsUtils.html2Text(attributeData.getValue().toString()));
TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue);
attributesDefs.add(turAttrDef);
} else if (attributeData != null && attributeData.getValue() != null) {
turMultiValue.add(attributeData.getValue().toString());
TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue);
attributesDefs.add(turAttrDef);
}

}
return attributesDefs;
}
}
return attributesDefs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,13 @@ private static void indexContentInstance(ManagedObject mo, IHandlerConfiguration
MappingDefinitions mappingDefinitions) {
try {
ContentInstance contentInstance = (ContentInstance) mo;

// When there is related content but no associated site.
if (MGMT_SITE.equals(siteName)) {
siteName = TuringUtils.getSiteName(contentInstance, config);
}

AsLocaleData asLocaleData = null;
if (hasLocale(contentInstance))
asLocaleData = contentInstance.getLocale().getAsLocale().getData();

TurSNSiteConfig turSNSiteConfig = config.getSNSiteConfig(siteName, asLocaleData);
String contentTypeName = contentInstance.getObjectType().getData().getName();
if (isCTDIntoMapping(contentTypeName, config)) {
if (mappingDefinitions.isClassValidToIndex(contentInstance, config)) {
log.info("Viglet Turing indexer Processing Content Type: {}, WEM Site: {}, SNSite: {}, Locale: {}",
contentTypeName, siteName, turSNSiteConfig.getName(), turSNSiteConfig.getLocale());
String xmlToIndex = generateXMLToIndex(contentInstance, config);
if (xmlToIndex.contains(FILE_PROTOCOL)) {
generateZipImport(xmlToIndex, turSNSiteConfig, config);
} else {
postIndex(xmlToIndex, turSNSiteConfig, config);
}
} else {
if (mappingDefinitions.hasClassValidToIndex(mo.getObjectType().getData().getName())
&& mo.getContentManagementId() != null) {
TurWEMDeindex.indexDelete(mo.getContentManagementId(), config, siteName);
}

}
indexFromMapping(mo, config, siteName, mappingDefinitions, contentInstance, contentTypeName);
} else {
if (log.isDebugEnabled())
log.debug("Mapping definition is not found in the mappingXML for the CTD and ignoring: {}", contentTypeName);
Expand All @@ -117,6 +95,34 @@ private static void indexContentInstance(ManagedObject mo, IHandlerConfiguration
}
}

private static void indexFromMapping(ManagedObject mo, IHandlerConfiguration config, String siteName,
MappingDefinitions mappingDefinitions, ContentInstance contentInstance,
String contentTypeName) throws Exception {
TurSNSiteConfig turSNSiteConfig = config.getSNSiteConfig(siteName, getAsLocaleData(contentInstance));
if (mappingDefinitions.isContentValidToIndex(contentInstance, config)) {
log.info("Viglet Turing indexer Processing Content Type: {}, WEM Site: {}, SNSite: {}, Locale: {}",
contentTypeName, siteName, turSNSiteConfig.getName(), turSNSiteConfig.getLocale());
String xmlToIndex = generateXMLToIndex(contentInstance, config);
if (xmlToIndex.contains(FILE_PROTOCOL)) {
generateZipImport(xmlToIndex, turSNSiteConfig, config);
} else {
postIndex(xmlToIndex, turSNSiteConfig, config);
}
} else {
if (mappingDefinitions.hasClassValidToIndex(mo.getObjectType().getData().getName())
&& mo.getContentManagementId() != null) {
TurWEMDeindex.indexDelete(mo.getContentManagementId(), config, siteName);
}
}
}

private static AsLocaleData getAsLocaleData(ContentInstance contentInstance) throws ApplicationException {
AsLocaleData asLocaleData = null;
if (hasLocale(contentInstance))
asLocaleData = contentInstance.getLocale().getAsLocale().getData();
return asLocaleData;
}

private static boolean hasLocale(ContentInstance contentInstance) throws ApplicationException {
return (contentInstance.getLocale() != null) && (contentInstance.getLocale().getAsLocale() != null)
&& (contentInstance.getLocale().getAsLocale().getData() != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private TurWEMUpdateFileWidget() {
}

public static List<TurAttrDef> attributeFileWidgetUpdate(TurAttrDefContext turAttrDefContext,
AttributeData attributeData) throws Exception {
AttributeData attributeData) {

TuringTag turingTag = turAttrDefContext.getTuringTag();
ContentInstance ci = turAttrDefContext.getContentInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at
} catch (ApplicationException e) {
log.error(e.getMessage(), e);
}
return null;
return new TurMultiValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package com.viglet.turing.connector.wem.ext;

import java.lang.invoke.MethodHandles;

import com.viglet.turing.connector.wem.beans.TurMultiValue;
import com.viglet.turing.connector.wem.beans.TuringTag;
import com.viglet.turing.connector.wem.config.IHandlerConfiguration;
Expand All @@ -31,6 +29,8 @@
import com.vignette.as.client.javabean.ContentInstance;
import com.vignette.logging.context.ContextLogger;

import java.lang.invoke.MethodHandles;

public class TurChannelPath implements ExtAttributeInterface {
private static final ContextLogger log = ContextLogger.getLogger(MethodHandles.lookup().lookupClass());

Expand All @@ -47,7 +47,7 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at
for (String s : breadcrumb) channelPath.append("/").append(s);
}
} catch (ApplicationException | AuthorizationException | ValidationException e) {
throw new RuntimeException(e);
log.error(e.getMessage(), e);
}
return TurMultiValue.singleItem(channelPath.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at
return turMultiValue;
}
} catch (ApplicationException | AuthorizationException | ValidationException | RemoteException e) {
throw new RuntimeException(e);
log.error(e.getMessage(), e);
}
return new TurMultiValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TurSpotlightExtraFields implements ExtAttributeInterface {
Expand Down Expand Up @@ -74,7 +75,7 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at
} catch (ApplicationException | JsonProcessingException e) {
log.error(e.getMessage(), e);
}
return null;
return new TurMultiValue();
}

static class TurCTDAttributes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,16 @@ public boolean hasClassValidToIndex(String contentTypeName) {
return status;
}

public IValidToIndex validToIndex(ObjectType ot, IHandlerConfiguration config) {

public IValidToIndex validToIndex(ObjectType ot) {
try {
String contentTypeName;
contentTypeName = ot.getData().getName();

if (this.hasClassValidToIndex(contentTypeName)) {
CTDMappings ctdMappings = turCTDMappingMap.get(contentTypeName);
IValidToIndex instance = null;
String className = ctdMappings.getClassValidToIndex();
if (className != null) {
Class<?> clazz = Class.forName(className);

if (clazz == null) {
if (logger.isDebugEnabled())
logger.debug(String.format("Valid to Index className is not found in the jar file: %s",
className));

} else
instance = (IValidToIndex) clazz.newInstance();
instance = (IValidToIndex) Class.forName(className).getDeclaredConstructor().newInstance();
}
return instance;
}
Expand All @@ -101,9 +91,9 @@ public IValidToIndex validToIndex(ObjectType ot, IHandlerConfiguration config) {

}

public boolean isClassValidToIndex(ContentInstance ci, IHandlerConfiguration config) {
public boolean isContentValidToIndex(ContentInstance ci, IHandlerConfiguration config) {
try {
IValidToIndex iValidToIndex = validToIndex(ci.getObjectType(), config);
IValidToIndex iValidToIndex = validToIndex(ci.getObjectType());
return !(iValidToIndex != null && !iValidToIndex.isValid(ci, config));
} catch (Exception e) {
logger.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public static MappingDefinitions loadMappings(String resourceXml) {

try {
DocumentBuilderFactory dlf = DocumentBuilderFactory.newInstance();
dlf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dlf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dlf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

DocumentBuilder db = dlf.newDocumentBuilder();

File f = new File(resourceXml);
Expand Down

0 comments on commit 8dafd01

Please sign in to comment.