Add purge action.

This commit is contained in:
Paulo Gustavo Veiga 2013-03-24 15:03:19 -03:00
parent f136240591
commit 811cc8a668

View File

@ -20,18 +20,29 @@ package com.wisemapping.rest;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.model.AuthenticationType;
import com.wisemapping.model.Collaboration;
import com.wisemapping.model.Mindmap;
import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestMindmapList;
import com.wisemapping.rest.model.RestUser;
import com.wisemapping.service.MindmapService;
import com.wisemapping.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.util.resources.CalendarData_th;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;
@Controller
public class AdminController extends BaseController {
@ -39,6 +50,11 @@ public class AdminController extends BaseController {
@Autowired
private UserService userService;
@Qualifier("mindmapService")
@Autowired
private MindmapService mindmapService;
@RequestMapping(method = RequestMethod.GET, value = "admin/users/{id}", produces = {"application/json", "text/html", "application/xml"})
@ResponseBody
public ModelAndView getUserById(@PathVariable long id) throws IOException {
@ -115,4 +131,54 @@ public class AdminController extends BaseController {
userService.deleteUser(user);
}
@RequestMapping(method = RequestMethod.GET, value = "admin/db/purge", consumes = {"text/plain"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void purgeDB(@RequestParam(required = true) int max) throws UnsupportedEncodingException, WiseMappingException {
for (int i = 0; i < max; i++) {
User user;
try {
user = userService.getUserBy(i);
} catch (Exception e) {
// User does not exit's continue ...
continue;
}
// Do not process admin accounts ...
if (user.getEmail().contains("wisemapping")) {
continue;
}
// Iterate over the list of maps ...
final List<Collaboration> collaborations = mindmapService.findCollaborations(user);
for (Collaboration collaboration : collaborations) {
final Mindmap mindmap = collaboration.getMindMap();
if (MindmapFilter.MY_MAPS.accept(mindmap, user)) {
final Calendar yearAgo = Calendar.getInstance();
yearAgo.add(Calendar.MONTH, -18);
// The use has only two maps... When they have been modified ..
if (mindmap.getLastModificationTime().before(yearAgo) && !mindmap.isPublic()) {
if (isWelcomeMap(mindmap) || isSimpleMap(mindmap)) {
mindmapService.removeMindmap(mindmap, user);
}
}
}
}
}
}
private boolean isWelcomeMap(@NotNull Mindmap mindmap) throws UnsupportedEncodingException {
// Is welcome map ?
final String xmlStr = mindmap.getXmlStr();
return xmlStr.contains("Welcome To") && xmlStr.contains("My Wisemaps");
}
public boolean isSimpleMap(@NotNull Mindmap mindmap) throws UnsupportedEncodingException {
String xmlStr = mindmap.getXmlStr();
String[] topics = xmlStr.split(Pattern.quote("<topic"));
return topics.length <= 3;
}
}