Admin page.

This commit is contained in:
marcins78@gmail.com
2011-12-10 19:11:15 +00:00
parent e7307bce65
commit d9eb5409ca
7 changed files with 162 additions and 44 deletions

View File

@@ -29,6 +29,11 @@ public class LeagueDAO {
_serializer = new CollectionSerializer(_library);
}
public void clearCache() {
_dateLoaded = 0;
_activeLeagues = null;
}
public void addLeague(String name, String type, CardCollection collection, int startTime, int endTime) throws SQLException, IOException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
@@ -98,6 +103,7 @@ public class LeagueDAO {
int currentDate = getCurrentDate();
if (currentDate != _dateLoaded) {
try {
_activeLeagues.clear();
loadActiveLeagues(currentDate);
_dateLoaded = currentDate;
} catch (SQLException e) {

View File

@@ -20,7 +20,7 @@ public abstract class AbstractResource {
protected final Player getResourceOwnerSafely(HttpServletRequest request, String participantId) {
String loggedUser = getLoggedUser(request);
if (_test)
if (_test && loggedUser == null)
loggedUser = participantId;
if (loggedUser == null)

View File

@@ -0,0 +1,72 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.vo.Player;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.sun.jersey.spi.resource.Singleton;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
@Singleton
@Path("/admin")
public class AdminResource extends AbstractResource {
@Context
private CollectionDAO _collectionDao;
@Context
private DeckDAO _deckDao;
@Context
private LeagueDAO _leagueDao;
@Context
private LotroCardBlueprintLibrary _library;
@Path("/clearCache")
@GET
public String clearCache(@Context HttpServletRequest request) throws Exception {
validateAdmin(request);
_playerDao.clearCache();
_collectionDao.clearCache();
_deckDao.clearCache();
_leagueDao.clearCache();
return "OK";
}
@Path("/addLeague")
@POST
public String addLeague(
@FormParam("name") String name,
@FormParam("type") String type,
@FormParam("start") int start,
@FormParam("end") int end,
@FormParam("packCollection") String packCollection,
@Context HttpServletRequest request) throws Exception {
validateAdmin(request);
DefaultCardCollection collection = new DefaultCardCollection(_library);
String[] packs = packCollection.split("\n");
for (String pack : packs)
collection.addPacks(pack, 1);
collection.finishedReading();
_leagueDao.addLeague(name, type, collection, start, end);
return "OK";
}
private void validateAdmin(HttpServletRequest request) {
Player player = getResourceOwnerSafely(request, null);
if (!player.getType().equals("a"))
sendError(Response.Status.FORBIDDEN);
}
}

View File

@@ -9,10 +9,7 @@ import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.LotroServer;
import com.gempukku.lotro.game.MutableCardCollection;
import com.gempukku.lotro.league.LeagueService;
import com.gempukku.lotro.packs.FixedPackBox;
import com.gempukku.lotro.packs.LeagueStarterBox;
import com.gempukku.lotro.packs.PacksStorage;
import com.gempukku.lotro.packs.RarityPackBox;
import com.sun.jersey.spi.resource.Singleton;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
@@ -26,7 +23,6 @@ import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
@Singleton
@@ -42,26 +38,9 @@ public class CollectionResource extends AbstractResource {
private LotroCardBlueprintLibrary _library;
@Context
private LeagueService _leagueService;
@Context
private PacksStorage _packStorage;
public CollectionResource() {
try {
_packStorage = new PacksStorage();
_packStorage.addPackBox("FotR - League Starter", new LeagueStarterBox());
_packStorage.addPackBox("FotR - Gandalf Starter", new FixedPackBox(_library, "FotR - Gandalf Starter"));
_packStorage.addPackBox("FotR - Aragorn Starter", new FixedPackBox(_library, "FotR - Aragorn Starter"));
_packStorage.addPackBox("FotR - Booster", new RarityPackBox(_library, 1));
} catch (IOException exp) {
_logger.error("Error while creating resource", exp);
exp.printStackTrace();
} catch (RuntimeException exp) {
_logger.error("Error while creating resource", exp);
exp.printStackTrace();
}
}
@Path("/{collectionType}")
@GET
@Produces(MediaType.APPLICATION_XML)

View File

@@ -0,0 +1,63 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.packs.FixedPackBox;
import com.gempukku.lotro.packs.LeagueStarterBox;
import com.gempukku.lotro.packs.PacksStorage;
import com.gempukku.lotro.packs.RarityPackBox;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;
import org.apache.log4j.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.lang.reflect.Type;
@Provider
public class PacksStorageProvider implements Injectable<PacksStorage>, InjectableProvider<Context, Type> {
private static final Logger _logger = Logger.getLogger(PacksStorageProvider.class);
private PacksStorage _packsStorage;
@Context
private LotroCardBlueprintLibrary _library;
@Override
public Injectable getInjectable(ComponentContext ic, Context context, Type type) {
if (type.equals(PacksStorage.class))
return this;
return null;
}
@Override
public ComponentScope getScope() {
return ComponentScope.Singleton;
}
private PacksStorage createPacksStorage() {
try {
PacksStorage packStorage = new PacksStorage();
packStorage.addPackBox("FotR - League Starter", new LeagueStarterBox());
packStorage.addPackBox("FotR - Gandalf Starter", new FixedPackBox(_library, "FotR - Gandalf Starter"));
packStorage.addPackBox("FotR - Aragorn Starter", new FixedPackBox(_library, "FotR - Aragorn Starter"));
packStorage.addPackBox("FotR - Booster", new RarityPackBox(_library, 1));
return packStorage;
} catch (IOException exp) {
_logger.error("Error while creating resource", exp);
exp.printStackTrace();
} catch (RuntimeException exp) {
_logger.error("Error while creating resource", exp);
exp.printStackTrace();
}
return null;
}
@Override
public synchronized PacksStorage getValue() {
if (_packsStorage == null)
_packsStorage = createPacksStorage();
return _packsStorage;
}
}

View File

@@ -2,8 +2,6 @@ package com.gempukku.lotro.server;
import com.gempukku.lotro.chat.ChatServer;
import com.gempukku.lotro.common.ApplicationRoot;
import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.GameHistoryDAO;
import com.gempukku.lotro.db.vo.GameHistoryEntry;
import com.gempukku.lotro.db.vo.Player;
@@ -45,10 +43,6 @@ public class ServerResource extends AbstractResource {
@Context
private ChatServer _chatServer;
@Context
private CollectionDAO _collectionDao;
@Context
private DeckDAO _deckDao;
@Context
private GameHistoryDAO _gameHistoryDao;
public ServerResource() {
@@ -62,21 +56,6 @@ public class ServerResource extends AbstractResource {
_logger.debug("Resource created");
}
@Path("/clearCache")
@GET
public String clearCache(@Context HttpServletRequest request) throws Exception {
Player player = getResourceOwnerSafely(request, null);
if (!player.getType().equals("a"))
sendError(Response.Status.FORBIDDEN);
_playerDao.clearCache();
_collectionDao.clearCache();
_deckDao.clearCache();
return "OK";
}
@Path("/login")
@POST
@Produces("text/html")

View File

@@ -0,0 +1,19 @@
<html>
<body>
<h1>Cache</h1>
<a href="server/admin/clearCache">Clear cache</a>
<h1>League</h1>
<h2>Add league</h2>
<form method="POST" action="server/admin/addLeague">
Name: <input type="text" name="name"><br/>
Type: <input type="text" name="type"><br/>
Start: <input type="text" name="start"><br/>
End: <input type="text" name="end"><br/>
Packs: <textarea rows="5" cols="20" name="packCollection"></textarea>
<input type="submit" value="Create">
</form>
</body>
</html>