Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 12999 delete content types #13214

Merged
merged 10 commits into from
Dec 19, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,35 @@ public static java.util.List<MultiTree> getContainerMultiTree(String containerId
throw new DotRuntimeException(e.toString());
}
}

/**
* Get a list of MultiTree for Contentlets using a specific Structure and specific Container
* @param containerIdentifier
* @param structureIdentifier
* @return List of MultiTree
*/
public static List<MultiTree> getContainerStructureMultiTree(String containerIdentifier, String structureInode) {
try {
HibernateUtil dh = new HibernateUtil(MultiTree.class);
StringBuilder query = new StringBuilder();
query.append("FROM mt IN CLASS com.dotmarketing.beans.MultiTree ");
query.append("WHERE (mt.parent1 = ? or mt.parent2 = ?) ");
query.append("AND EXISTS (FROM c IN class com.dotmarketing.portlets.contentlet.business.Contentlet ");
query.append(" WHERE c.identifier = mt.child AND c.structureInode = ? )");

dh.setQuery(query.toString());
dh.setParam(containerIdentifier);
dh.setParam(containerIdentifier);
dh.setParam(structureInode);

return dh.list();

} catch (Exception e) {
Logger.error(MultiTreeFactory.class, "getContainerStructureMultiTree failed:" + e, e);
throw new DotRuntimeException(e.toString());
}
}

@SuppressWarnings("unchecked")
public static java.util.List<MultiTree> getMultiTreeByChild(String contentIdentifier) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.dotmarketing.portlets.containers.action;

import com.dotmarketing.beans.MultiTree;
import com.dotmarketing.business.FactoryLocator;
import com.dotmarketing.factories.MultiTreeFactory;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -559,12 +563,16 @@ public void _saveWebAsset(ActionRequest req, ActionResponse res,
identifier.setHostId(host.getIdentifier());
APILocator.getIdentifierAPI().save(identifier);


List<ContainerStructure> oldContainerStructures = APILocator.getContainerAPI().getContainerStructures(currentContainer);
List<ContainerStructure> csList = new LinkedList<>();

// saving the multiple structures
if(container.getMaxContentlets()>0) {
String structuresIdsStr = req.getParameter("structuresIds");

String[] structuresIds = structuresIdsStr.split("#");
List<ContainerStructure> csList = new LinkedList<ContainerStructure>();
List<String> unusedStructures = new ArrayList<>();

for (String structureId : structuresIds) {
String code = req.getParameter("code_"+structureId);
Expand All @@ -576,10 +584,30 @@ public void _saveWebAsset(ActionRequest req, ActionResponse res,
csList.add(cs);
}

//Save new structures
APILocator.getContainerAPI().saveContainerStructures(csList);

}

//Delete MultiTree for old / unused ContainerStructures
for (ContainerStructure oldCS : oldContainerStructures) {
boolean unused = true;
for (ContainerStructure newCS : csList) {
if (newCS.getStructureId().equals(oldCS.getStructureId())) {
unused = false;
break;
}
}

if (unused) {
List<MultiTree> multiTreeList = MultiTreeFactory
.getContainerStructureMultiTree(oldCS.getContainerId(), oldCS.getStructureId());
for (MultiTree mt : multiTreeList) {
MultiTreeFactory.deleteMultiTree(mt);
}
}
}


SessionMessages.add(httpReq, "message", "message.containers.save");
ActivityLogger.logInfo(this.getClass(), "Save WebAsset action", "User " + user.getPrimaryKey() + " saved " + container.getTitle(), HostUtil.hostNameUtil(req, _getUser(req)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ public void deleteContainerContentTypesByContainerInode(final Container containe
List<Container> findContainersForStructure(String structureInode)
throws DotDataException;

/**
* Retrieves containers using the specified structure
*
* @param structureInode
* @param workingOrLiveOnly
* @return
* @throws DotDataException
*/
List<Container> findContainersForStructure(String structureInode, boolean workingOrLiveOnly)
throws DotDataException;

/**
*
* @param assetsOlderThan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,14 @@ public List<Container> findContainersForStructure(String structureInode) throws
return containerFactory.findContainersForStructure(structureInode);
}

@Override
@CloseDBIfOpened
@Override
public List<Container> findContainersForStructure(String structureInode,
boolean workingOrLiveOnly) throws DotDataException {
return containerFactory.findContainersForStructure(structureInode, workingOrLiveOnly);
}

@Override
public int deleteOldVersions(Date assetsOlderThan) throws DotStateException, DotDataException {
return deleteOldVersions(assetsOlderThan, Inode.Type.CONTAINERS.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ public interface ContainerFactory {
*/
public List<Container> findContainers(User user, boolean includeArchived, Map<String,Object> params, String hostId, String inode, String identifier, String parent, int offset, int limit, String orderBy) throws DotSecurityException, DotDataException;

public List<Container> findContainersForStructure(String structureInode) throws DotDataException;
public List<Container> findContainersForStructure(String structureIdentifier) throws DotDataException;

/**
* Search Containers associated with a specific Structure
* @param structureIdentifier
* @param workingOrLiveOnly True to filter the Containers if the version associated with the Structure is live or Working only
* @return list of container
* @throws DotDataException
*/
public List<Container> findContainersForStructure(String structureIdentifier, boolean workingOrLiveOnly) throws DotDataException;

/**
* Method will replace user references of the given userId in containers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.containers.model.Container;
import com.dotmarketing.portlets.containers.model.ContainerVersionInfo;
import com.dotmarketing.util.ConvertToPOJOUtil;
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.Logger;
Expand Down Expand Up @@ -309,22 +310,35 @@ else if(resultList.size() < internalLimit)
return assets;
}

public List<Container> findContainersForStructure(String structureInode) throws DotDataException {
HibernateUtil dh = new HibernateUtil(Container.class);
public List<Container> findContainersForStructure(String structureIdentifier) throws DotDataException {
return findContainersForStructure(structureIdentifier, false);
}

StringBuilder query = new StringBuilder();
@Override
public List<Container> findContainersForStructure(String structureIdentifier,
boolean workingOrLiveOnly) throws DotDataException {
HibernateUtil dh = new HibernateUtil(Container.class);

query.append("FROM c IN CLASS ");
StringBuilder query = new StringBuilder();

query.append("FROM c IN CLASS ");
query.append(Container.class);
query.append(" WHERE exists ( from cs in class ");
query.append(ContainerStructure.class.getName());
query.append(" where cs.containerId = c.identifier and cs.structureId = ? ) ");
dh.setQuery(query.toString());
dh.setParam(structureInode);
return dh.list();
}

/**
query.append(" where cs.containerId = c.identifier and cs.structureId = ? ");
if (workingOrLiveOnly) {
query.append(" AND EXISTS ( FROM vi IN CLASS ");
query.append(ContainerVersionInfo.class.getName());
query.append(" WHERE vi.identifier = c.identifier AND ");
query.append(" (cs.containerInode = vi.workingInode OR cs.containerInode = vi.liveInode ) ) ");
}
query.append(") ");
dh.setQuery(query.toString());
dh.setParam(structureIdentifier);
return dh.list();
}

/**
* Method will replace user references of the given userId in containers
* with the replacement user id
* @param userId User Identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public Map<Object,Object> checkDependencies(String structureInode) throws DotDat
List<Map<String,String>> containersList = new ArrayList<Map<String,String>>();

// checking if there are containers using this structure
List<Container> containers=APILocator.getContainerAPI().findContainersForStructure(structureInode);
List<Container> containers=APILocator.getContainerAPI().findContainersForStructure(structureInode, true);
Map<String, Container> containersInUse = new HashMap<String, Container>();

for(Container c : containers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2170,7 +2170,7 @@ message.field.values=The <i>Values</i> field is mandatory when the Display Type
message.structure.savestructure=The Content Type has been saved
message.structure.deletestructure=The Content Type has been deleted
message.structure.notdeletestructure=The default Content Type could not be deleted
message.structure.notdeletestructure.container=The Content Type cannot be deleted because it used by the following Containers:
message.structure.notdeletestructure.container=The Content Type cannot be deleted because it is being used by the following Containers:
message.structure.cantdelete=Delete Content Type
message.structure.savefield=The Field has been saved.
message.structure.nodatatype=You have reached the maximum number of fields of this Data Type in this Content Type; please choose another Display Type.
Expand Down