Changeset 27806 for main/trunk


Ignore:
Timestamp:
2013-07-11T15:36:11+12:00 (11 years ago)
Author:
sjm84
Message:

Fixed some bugs and it now keeps track of where you are up to

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/DepositorAction.java

    r27719 r27806  
    44import java.io.File;
    55import java.io.FileWriter;
    6 import java.io.IOException;
    76import java.io.Serializable;
     7import java.lang.reflect.Type;
    88import java.util.ArrayList;
    99import java.util.HashMap;
    1010import java.util.Iterator;
     11import java.util.List;
     12import java.util.Map;
    1113
    1214import javax.xml.parsers.DocumentBuilderFactory;
     
    2729import org.w3c.dom.Node;
    2830
     31import com.google.gson.Gson;
     32import com.google.gson.reflect.TypeToken;
     33
    2934public class DepositorAction extends Action
    3035{
    3136    //Sub actions
    32     private final String DE_RETRIEVE_WIZARD = "getWizard";
    33     private final String DE_DEPOSIT_FILE = "depositFile";
     37    private final String DE_RETRIEVE_WIZARD = "getwizard";
     38    private final String DE_DEPOSIT_FILE = "depositfile";
     39    private final String DE_CLEAR_DATABASE = "cleardatabase";
    3440
    3541    public Node process(Node message)
     
    8591        }
    8692
     93        int highestVisitedPage = -1;
     94        String result = "";
     95        int counter = 1;
     96        while (result != null)
     97        {
     98            result = database.getUserData(currentUsername, "DE___" + collection + "___" + counter + "___VISITED_PAGE");
     99            if (result != null)
     100            {
     101                counter++;
     102            }
     103        }
     104        highestVisitedPage = counter - 1;
     105        if (highestVisitedPage == 0)
     106        {
     107            highestVisitedPage = 1;
     108        }
     109
     110        if (pageNum > highestVisitedPage + 1)
     111        {
     112            pageNum = highestVisitedPage + 1;
     113        }
     114
     115        database.addUserData(currentUsername, "DE___" + collection + "___" + pageNum + "___VISITED_PAGE", "VISITED");
     116
    87117        String subaction = ((Element) request).getAttribute(GSXML.SUBACTION_ATT);
    88         if (subaction.equals(DE_RETRIEVE_WIZARD))
     118        if (subaction.toLowerCase().equals(DE_RETRIEVE_WIZARD))
    89119        {
    90120            //Save given metadata
     
    171201                    pageLi.setAttribute("class", "wizardStepLink ui-state-active ui-corner-all");
    172202                }
     203                else if (i + 1 > highestVisitedPage + 1 && i + 1 > pageNum + 1)
     204                {
     205                    pageLi.setAttribute("class", "wizardStepLink ui-state-disabled ui-corner-all");
     206                }
    173207                else
    174208                {
     
    233267            database.closeDatabase();
    234268        }
    235         else if (subaction.equals(DE_DEPOSIT_FILE))
     269        else if (subaction.toLowerCase().equals(DE_DEPOSIT_FILE))
    236270        {
    237271            String fileToAdd = (String) params.get("fileToAdd");
     
    257291                }
    258292
    259                 String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\"><DirectoryMetadata><FileSet>";
    260                 xmlString += "<FileName>" + fileToAdd + "</FileName><Description>";
    261                 Iterator<String> paramIter = params.keySet().iterator();
    262                 while (paramIter.hasNext())
    263                 {
    264                     String paramName = paramIter.next();
    265                     if (paramName.startsWith("md___"))
     293                HashMap<String, String> metadataMap = new HashMap<String, String>();
     294                for (int i = pageNum; i > 0; i--)
     295                {
     296                    String cachedValues = database.getUserData(currentUsername, "DE___" + collection + "___" + i + "___CACHED_VALUES");
     297                    if (cachedValues != null)
    266298                    {
    267                         Object paramValue = params.get(paramName);
    268 
    269                         if (paramValue instanceof String)
     299                        Type type = new TypeToken<List<Map<String, String>>>()
    270300                        {
    271                             xmlString += "<Metadata name=\"" + paramName.substring(5) + "\" mode=\"accumulate\">" + (String) paramValue + "</Metadata>";
    272                         }
    273                         else if (paramValue instanceof HashMap)
     301                        }.getType();
     302
     303                        Gson gson = new Gson();
     304                        List<Map<String, String>> metadataList = gson.fromJson(cachedValues, type);
     305                        for (Map<String, String> metadata : metadataList)
    274306                        {
    275                             HashMap<String, String> subMap = (HashMap<String, String>) paramValue;
    276                             Iterator<String> subKeyIter = subMap.keySet().iterator();
    277                             while (subKeyIter.hasNext())
    278                             {
    279                                 String subName = subKeyIter.next();
    280                                 xmlString += "<Metadata name=\"" + paramName.substring(5) + "." + subName + "\" mode=\"accumulate\">" + subMap.get(subName) + "</Metadata>";
    281                             }
     307                            metadataMap.put(metadata.get("name"), metadata.get("value"));
    282308                        }
    283309                    }
    284310                }
    285                 xmlString += "</Description></FileSet><DirectoryMetadata>";
     311
     312                String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\"><DirectoryMetadata><FileSet>";
     313                xmlString += "<FileName>.*</FileName><Description>";
     314                for (String key : metadataMap.keySet())
     315                {
     316                    xmlString += "<Metadata name=\"" + key.substring("MD___".length()) + "\" mode=\"accumulate\">" + metadataMap.get(key) + "</Metadata>";
     317                }
     318                xmlString += "</Description></FileSet></DirectoryMetadata>";
    286319
    287320                File metadataFile = new File(GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + this.config_params.get(GSConstants.SITE_NAME) + File.separator + "collect" + File.separator + collection + File.separator + "import" + File.separator + fileToAdd + File.separator + "metadata.xml");
     
    317350                Element buildResponseMessage = (Element) this.mr.process(buildMessage);
    318351
    319                 System.err.println("RESPONSE = " + GSXML.xmlNodeToString(buildResponseMessage));
    320                
    321352                response.appendChild(this.doc.importNode(buildResponseMessage, true));
    322353            }
     354        }
     355        else if (subaction.toLowerCase().equals(DE_CLEAR_DATABASE))
     356        {
     357            database.clearUserData();
     358            System.err.println("\n\nCLEARED\n\n");
    323359        }
    324360        else
Note: See TracChangeset for help on using the changeset viewer.