Example usage for org.apache.poi.ss.usermodel Workbook getSheetName

List of usage examples for org.apache.poi.ss.usermodel Workbook getSheetName

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Workbook getSheetName.

Prototype

String getSheetName(int sheet);

Source Link

Document

Get the sheet name

Usage

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addAssociations(Registry registry, File dataDirectory) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(dataDirectory, "association");
    for (Workbook workbook : workbooks) {
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }//from  w ww.j  a va  2 s .co m
        int limit = sheet.getLastRowNum();
        for (int i = 0; i <= limit; i++) {
            Row row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            registry.addAssociation(getCellValue(row.getCell(0), null), getCellValue(row.getCell(1), null),
                    getCellValue(row.getCell(2), null));
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addCommentsRatingsAndTags(Registry registry, File dataDirectory) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(dataDirectory, "community");
    for (Workbook workbook : workbooks) {
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }/*from  ww  w.j av  a 2  s  .  co  m*/
        int limit = sheet.getLastRowNum();
        for (int i = 0; i <= limit; i++) {
            Row row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            String type = getCellValue(row.getCell(0), null).toLowerCase();
            if (type.contains("tag")) {
                String tag = getCellValue(row.getCell(2), null);
                if (tag == null) {
                    String[] parts = getCellValue(row.getCell(1), null).split(";");
                    registry.removeTag(parts[0], parts[1].substring(5));
                } else {
                    registry.applyTag(getCellValue(row.getCell(1), null), tag);
                }
            } else if (type.contains("comment")) {
                String path = getCellValue(row.getCell(1), null);
                if (path.contains(";")) {
                    String comment = getCellValue(row.getCell(2), null);
                    if (comment == null) {
                        registry.removeComment(path);
                    } else {
                        registry.editComment(path, comment);
                    }
                } else {
                    registry.addComment(path, new Comment(getCellValue(row.getCell(2), null)));
                }
            } else if (type.contains("rate") || type.contains("rating")) {
                registry.rateResource(getCellValue(row.getCell(1), null),
                        Integer.parseInt(getCellValue(row.getCell(2), "0")));
            }
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void importAndUploadResources(Registry registry, File dataDirectory) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(dataDirectory, "import");
    for (Workbook workbook : workbooks) {
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }/*from ww w.j  a v  a2 s.c o m*/
        int limit = sheet.getLastRowNum();
        for (int i = 0; i <= limit; i++) {
            Row row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            String path = row.getCell(0).getStringCellValue();
            String url = row.getCell(1).getStringCellValue();
            Resource resource = registry.newResource();
            resource.setMediaType(getCellValue(row.getCell(2), null));
            resource.setDescription(getCellValue(row.getCell(3),
                    "This resource was added using the " + "WSO2 Governance Registry Sample Data Populator"));
            if (url.startsWith("file:")) {
                resource.setContentStream(new BufferedInputStream(new FileInputStream(new File(new URI(url)))));
                registry.put(path, resource);
            } else {
                registry.importResource(path, url, resource);
            }
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addResourcesAndCollections(Registry registry, File dataDirectory) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(dataDirectory, "resource");
    for (Workbook workbook : workbooks) {
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }/*  w  ww .j  a v  a 2s .c o  m*/
        int limit = sheet.getLastRowNum();
        for (int i = 0; i <= limit; i++) {
            Row row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            String path = row.getCell(0).getStringCellValue();
            Resource resource;
            if (registry.resourceExists(path)) {
                resource = registry.get(path);
                String key = getCellValue(row.getCell(1), null);
                String value = getCellValue(row.getCell(2), null);
                if (value == null) {
                    resource.removeProperty(key);
                } else {
                    resource.setProperty(key, value);
                }
            } else {
                String value = getCellValue(row.getCell(1), null);
                if (value == null) {
                    resource = registry.newCollection();
                } else {
                    resource = registry.newResource();
                    resource.setMediaType("text/plain");
                    resource.setContent(value);
                }
                resource.setDescription(getCellValue(row.getCell(2), "This resource was added using the "
                        + "WSO2 Governance Registry Sample Data Populator"));
            }
            registry.put(path, resource);
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addAssets(Registry registry, File dataDirectory) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(dataDirectory, "asset");
    for (Workbook workbook : workbooks) {
        Registry governanceRegistry = GovernanceUtils.getGovernanceUserRegistry(registry,
                CommandHandler.getUsername());
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }/*from  www.  j av  a  2 s  .  c  o m*/
        int limit = sheet.getLastRowNum();
        if (limit < 1) {
            throw new RuntimeException("Column headers were not specified in Asset Data Spreadsheet");
        }
        Row row = sheet.getRow(0);
        int key = -1;
        List<String> temp = new LinkedList<String>();
        String value;
        int count = 0;
        while ((value = getCellValue(row.getCell(count++), null)) != null) {
            if (value.equals("key")) {
                key = count - 1;
            } else {
                temp.add(value);
            }
        }
        String[] headers = temp.toArray(new String[temp.size()]);
        if (key == -1) {
            throw new RuntimeException("Asset Key was not specified");
        }
        for (int i = 1; i <= limit; i++) {
            row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            String type = row.getCell(key).getStringCellValue();
            String nameAttribute = GovernanceUtils.findGovernanceArtifactConfiguration(type, governanceRegistry)
                    .getArtifactNameAttribute();
            String namespaceAttribute = GovernanceUtils
                    .findGovernanceArtifactConfiguration(type, governanceRegistry)
                    .getArtifactNamespaceAttribute();
            GenericArtifactManager manager = new GenericArtifactManager(governanceRegistry, type);
            Map<String, String> attributeMap = new HashMap<String, String>();
            for (int j = 0; j < headers.length; j++) {
                attributeMap.put(headers[j], row.getCell(j > key ? j + 1 : j).getStringCellValue());
            }
            GenericArtifact artifact = manager.newGovernanceArtifact(new QName(
                    attributeMap.containsKey(namespaceAttribute) ? attributeMap.get(namespaceAttribute) : null,
                    attributeMap.containsKey(nameAttribute) ? attributeMap.get(nameAttribute)
                            : UUIDGenerator.generateUUID()));
            for (Map.Entry<String, String> e : attributeMap.entrySet()) {
                artifact.setAttribute(e.getKey(), e.getValue());
            }
            manager.addGenericArtifact(artifact);
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addSubscriptions(ConfigurationContext configContext) throws Exception {
    File subscriptionsDirectory = new File(CommandHandler.getSubscriptionsLocation());
    if (subscriptionsDirectory.exists()) {
        SubscriberClient manager = new SubscriberClient(cookie, CommandHandler.getServiceURL(), configContext);
        Workbook workbook = PopulatorUtil.getWorkbook(subscriptionsDirectory, "list");
        if (workbook != null) {
            Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
            if (sheet == null || sheet.getLastRowNum() == -1) {
                throw new RuntimeException("The first sheet is empty");
            }/*  w  ww .  j a  va 2s.c o m*/
            int limit = sheet.getLastRowNum();
            for (int i = 0; i <= limit; i++) {
                Row row = sheet.getRow(i);
                if (row == null || row.getCell(0) == null) {
                    break;
                }
                manager.subscribe(row.getCell(0).getStringCellValue(), row.getCell(1).getStringCellValue(),
                        row.getCell(2).getStringCellValue());
            }
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void performLifecycleOperations(ConfigurationContext configContext, Registry registry)
        throws Exception {
    if (CommandHandler.getLifecycleConfigLocation() != null) {
        File lifecycleOperationsLocation = new File(CommandHandler.getLifecycleOperationsLocation());
        LifeCycleManagementClient client = new LifeCycleManagementClient(cookie, CommandHandler.getServiceURL(),
                configContext);//from w  w w . j  a  v  a  2s.  c om
        if (lifecycleOperationsLocation.exists()) {
            Workbook[] workbooks = PopulatorUtil.getWorkbooks(lifecycleOperationsLocation, "actions");
            for (Workbook workbook : workbooks) {
                Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
                if (sheet == null || sheet.getLastRowNum() == -1) {
                    throw new RuntimeException("The first sheet is empty");
                }
                int limit = sheet.getLastRowNum();
                for (int i = 0; i <= limit; i++) {
                    Row row = sheet.getRow(i);
                    if (row == null || row.getCell(0) == null) {
                        break;
                    }
                    String path = getCellValue(row.getCell(0), null);
                    String aspect = getCellValue(row.getCell(1), null);
                    String action = getCellValue(row.getCell(2), null);
                    if (action == null) {
                        if (aspect.equals(registry.get(path).getProperty("registry.LC.name"))) {
                            client.removeAspect(path, aspect);
                        } else {
                            client.addAspect(path, aspect);
                        }
                    } else {
                        String[] items = splitAndTrim(getCellValue(row.getCell(3), null), ",");
                        String temp = getCellValue(row.getCell(4), null);
                        if (temp == null) {
                            client.invokeAspect(path, aspect, action, items,
                                    Collections.<String, String>emptyMap());
                        } else {
                            Map<String, String> params = new LinkedHashMap<String, String>();
                            String[] pairs = splitAndTrim(temp, ",");
                            for (String pair : pairs) {
                                String[] keyAndValue = splitAndTrim(pair, "[|]");
                                params.put(keyAndValue[0], keyAndValue[1]);
                            }
                            client.invokeAspect(path, aspect, action, items, params);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addUsers(File usersAndRolesDirectory, UserManagementClient userManager) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(usersAndRolesDirectory, "users");
    for (Workbook workbook : workbooks) {
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }/*www  . j  a  va2  s . c o m*/
        int limit = sheet.getLastRowNum();
        for (int i = 0; i <= limit; i++) {
            Row row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            String name = row.getCell(0).getStringCellValue();
            String password = getCellValue(row.getCell(1), name + "123");
            String roles = getCellValue(row.getCell(2), null);
            userManager.addUser(name, password, splitAndTrim(roles == null ? null : roles, ","),
                    new ClaimValue[0], null);
            if (row.getCell(3) != null && row.getCell(3).getCellType() != Cell.CELL_TYPE_BLANK) {
                UserProfileDTO profile = userManager.getUserProfile(name, "default");
                UserFieldDTO[] fieldValues = profile.getFieldValues();
                String email = row.getCell(3).getStringCellValue();
                String firstName = getCellValue(row.getCell(4), name);
                String lastName = getCellValue(row.getCell(5), name);
                for (UserFieldDTO fieldValue : fieldValues) {
                    if (fieldValue.getClaimUri().equals("http://wso2.org/claims/emailaddress")) {
                        fieldValue.setFieldValue(email);
                    } else if (fieldValue.getClaimUri().equals("http://wso2.org/claims/givenname")) {
                        fieldValue.setFieldValue(firstName);
                    } else if (fieldValue.getClaimUri().equals("http://wso2.org/claims/lastname")) {
                        fieldValue.setFieldValue(lastName);
                    } else {
                        fieldValue.setFieldValue("");
                    }
                }
                userManager.setUserProfile(name, profile);
            }
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addRoles(File usersAndRolesDirectory, UserManagementClient userManager) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(usersAndRolesDirectory, "roles");
    for (Workbook workbook : workbooks) {
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }//  w  w  w.j av  a 2s.co  m
        int limit = sheet.getLastRowNum();
        for (int i = 0; i <= limit; i++) {
            Row row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            String name = row.getCell(0).getStringCellValue();
            String[] permissions = splitAndTrim(row.getCell(1).getStringCellValue(), ",");
            if (name.equals("everyone") || name.equals("admin")) {
                userManager.setRoleUIPermission(name, permissions);
            } else {
                userManager.addRole(name, new String[0], permissions);
            }
        }
    }
}

From source file:org.wso2.carbon.registry.samples.populator.Main.java

License:Open Source License

private static void addTenants(File usersAndRolesDirectory, UserManagementClient userManager) throws Exception {
    Workbook[] workbooks = PopulatorUtil.getWorkbooks(usersAndRolesDirectory, "tenants");
    for (Workbook workbook : workbooks) {
        Sheet sheet = workbook.getSheet(workbook.getSheetName(0));
        if (sheet == null || sheet.getLastRowNum() == -1) {
            throw new RuntimeException("The first sheet is empty");
        }//  ww w .  j av  a2  s. c  o m
        int limit = sheet.getLastRowNum();
        for (int i = 0; i <= limit; i++) {
            Row row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                break;
            }
            userManager.addTenant(getCellValue(row.getCell(0), null), getCellValue(row.getCell(1), null),
                    getCellValue(row.getCell(2), null), getCellValue(row.getCell(3), null),
                    getCellValue(row.getCell(4), null), getCellValue(row.getCell(5), null));
        }
    }
}