-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
519 additions
and
535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
dotCMS/src/main/java/com/dotcms/util/transform/DBTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.dotcms.util.transform; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Interface that contains the definition that a transformer needs to implement in order to convert | ||
* DB objects into entities(eg. Folder, Template, Containers) | ||
* @param <T> | ||
*/ | ||
public interface DBTransformer<T> { | ||
|
||
/** | ||
* @return List of converted objects | ||
*/ | ||
List<T> asList(); | ||
} |
124 changes: 124 additions & 0 deletions
124
dotCMS/src/main/java/com/dotcms/util/transform/TransformerLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.dotcms.util.transform; | ||
|
||
import com.dotmarketing.beans.Identifier; | ||
import com.dotmarketing.beans.transform.IdentifierTransformer; | ||
import com.dotmarketing.portlets.containers.model.Container; | ||
import com.dotmarketing.portlets.containers.transform.ContainerTransformer; | ||
import com.dotmarketing.portlets.folders.model.Folder; | ||
import com.dotmarketing.portlets.folders.transform.TemplateTransformer; | ||
import com.dotmarketing.portlets.links.model.Link; | ||
import com.dotmarketing.portlets.links.transform.LinkTransformer; | ||
import com.dotmarketing.portlets.templates.model.Template; | ||
import com.dotmarketing.portlets.templates.transform.FolderTransformer; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Factory class used to instantiate DBTransformer objects | ||
* Default DBTransformer objects supported so far are: FolderTransformer, TemplateTransformer, | ||
* ContainerTransformer, LinkTransformer and IdentifierTransformer | ||
* However, new implementations can be included calling the addTransformer | ||
* method and removed calling removeTransformer. | ||
* @author nollymar | ||
*/ | ||
public class TransformerLocator { | ||
|
||
private TransformerLocator() { | ||
|
||
} | ||
|
||
/** | ||
* Map that contains DbTransformer implementations | ||
*/ | ||
private static Map<Class, Function<List<Map<String, Object>>, DBTransformer>> transformerMapping = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
transformerMapping.put (Folder.class, TransformerLocator::createFolderTransformer); | ||
transformerMapping.put (Template.class, TransformerLocator::createTemplateTransformer); | ||
transformerMapping.put (Container.class, TransformerLocator::createContainerTransformer); | ||
transformerMapping.put (Link.class, TransformerLocator::createLinkTransformer); | ||
transformerMapping.put (Identifier.class, TransformerLocator::createIdentifierTransformer); | ||
} | ||
|
||
public static DBTransformer createDBTransformer(List<Map<String, Object>> list, Class clazz) { | ||
DBTransformer transformer = null; | ||
|
||
if (transformerMapping.containsKey(clazz)){ | ||
transformer = transformerMapping.get(clazz).apply(list); | ||
} | ||
return transformer; | ||
} | ||
|
||
/** | ||
* Extends default DBTransformer map | ||
* @param clazz Class which DB results will be transformed to | ||
* @param transformerFunction DBTransformer that contains the implementation | ||
*/ | ||
public static void addTransformer(Class clazz, | ||
Function<List<Map<String, Object>>, DBTransformer> transformerFunction) { | ||
transformerMapping.put(clazz, transformerFunction); | ||
} | ||
|
||
/** | ||
* Remove a DBTransformer implementation from the map given the class as a key | ||
* @param clazz | ||
*/ | ||
public static void removeTransformer(Class clazz){ | ||
transformerMapping.remove(clazz); | ||
} | ||
|
||
/** | ||
* Creates a DBTransformer for Folder objects | ||
* @param initList List of DB results to be transformed | ||
* @return | ||
*/ | ||
public static FolderTransformer createFolderTransformer(List<Map<String, Object>> initList) { | ||
|
||
return new FolderTransformer(initList); | ||
} | ||
|
||
/** | ||
* Creates a DBTransformer for Template objects | ||
* @param initList List of DB results to be transformed | ||
* @return | ||
*/ | ||
public static TemplateTransformer createTemplateTransformer( | ||
List<Map<String, Object>> initList) { | ||
|
||
return new TemplateTransformer(initList); | ||
} | ||
|
||
/** | ||
* Creates a DBTransformer for Container objects | ||
* @param initList List of DB results to be transformed | ||
* @return | ||
*/ | ||
public static ContainerTransformer createContainerTransformer( | ||
List<Map<String, Object>> initList) { | ||
|
||
return new ContainerTransformer(initList); | ||
} | ||
|
||
/** | ||
* Creates a DBTransformer for Link objects | ||
* @param initList List of DB results to be transformed | ||
* @return | ||
*/ | ||
public static LinkTransformer createLinkTransformer(List<Map<String, Object>> initList) { | ||
|
||
return new LinkTransformer(initList); | ||
} | ||
|
||
/** | ||
* Creates a DBTransformer for Identifier objects | ||
* @param initList List of DB results to be transformed | ||
* @return | ||
*/ | ||
public static IdentifierTransformer createIdentifierTransformer( | ||
List<Map<String, Object>> initList) { | ||
|
||
return new IdentifierTransformer(initList); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
dotCMS/src/main/java/com/dotmarketing/beans/transform/IdentifierTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.dotmarketing.beans.transform; | ||
|
||
import com.dotcms.util.transform.DBTransformer; | ||
import com.dotmarketing.beans.Identifier; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* DBTransformer that converts DB objects into Identifier instances | ||
*/ | ||
public class IdentifierTransformer implements DBTransformer { | ||
final List<Identifier> list; | ||
|
||
|
||
public IdentifierTransformer(List<Map<String, Object>> initList){ | ||
List<Identifier> newList = new ArrayList<>(); | ||
if (initList != null){ | ||
for(Map<String, Object> map : initList){ | ||
newList.add(transform(map)); | ||
} | ||
} | ||
|
||
this.list = newList; | ||
} | ||
|
||
@Override | ||
public List<Identifier> asList() { | ||
|
||
return this.list; | ||
} | ||
|
||
@NotNull | ||
private static Identifier transform(Map<String, Object> map) { | ||
final Identifier i = new Identifier(); | ||
i.setAssetName((String) map.get("asset_name")); | ||
i.setAssetType((String) map.get("asset_type")); | ||
i.setHostId((String) map.get("host_inode")); | ||
i.setId((String) map.get("id")); | ||
i.setParentPath((String) map.get("parent_path")); | ||
i.setSysPublishDate((Date) map.get("syspublish_date")); | ||
i.setSysExpireDate((Date) map.get("sysexpire_date")); | ||
return i; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.