Back to The tech awesomeness
Table of contents
Java chapters

The article for today.

English
Noun
impex (uncountable)
(business, in company names) import and export
https://en.wiktionary.org/wiki/impex

Español. Spanish
Noun
jira f (plural jiras)
A large piece torn from a fabric.

Noun
jira f (plural jiras)
picnic
https://en.wiktionary.org/wiki/jira

हिंदी. Hindi
Noun
जीरा • (jīrā) m (Urdu spelling جیرا‎)
cumin
https://en.wiktionary.org/wiki/जीरा#Hindi


org.mnode.ical4j:ical4j:1.0.4
com.atlassian.jira:jira-rest-java-client-app:5.2.0
org.slf4j:slf4j-log4j12:1.7.25
ch.qos.logback:logback-classic:1.2.3
org.apache.maven.plugins:maven-compiler-plugin:3.8.1

public class iCalExport {
    private static final String JIRA_URI = "https://127.0.0.1:8443";
    private static final String JIRA_USERNAME = "username";
    private static final String JIRA_USERPASSWORD = "password";
    private static final String APPLE_CALENDAR_FILE_LOCATION =
            "/Users/user/Desktop/APPLE.ics";
    private static final String ICAL_EXPORT_FILENAME =
            "/Users/user/Desktop/export_ical.ics";
    
    public static void main(String[] args) {
        List<Map<String, String>> items = new ArrayList<>();
        
        try {
            /* to export */
            Map map = exportFromJira();
            iCal iCal = new iCal();
            iCal.write(ICAL_EXPORT_FILENAME, map);
            
            /* to import */
            CalendarBuilder calendarBuilder = new CalendarBuilder();
            CompatibilityHints.setHintEnabled(
                    CompatibilityHints.KEY_RELAXED_UNFOLDING, true);
            CompatibilityHints.setHintEnabled(
                    CompatibilityHints.KEY_RELAXED_PARSING, true);

            File file = new File(APPLE_CALENDAR_FILE_LOCATION);

            FileInputStream fin = new FileInputStream(file);
            net.fortuna.ical4j.model.Calendar calendar =
                    calendarBuilder.build(fin);

            Map<String, String> result2 = new HashMap<>();
            Map<String, String> result3 = new HashMap<>();
            Map<String, String> result4 = new HashMap<>();
            
            Set<String> uniquePropertyNames = new HashSet<>();
            
            for (Iterator componentIterator
                    = calendar.getComponents().iterator();
                    componentIterator.hasNext();) {
                Map<String, String> info = new HashMap<>();
                Component component = (Component) componentIterator.next();
                
                Map<String, String>  item = new HashMap<>();

                for (Iterator propertyIterator =
                        component.getProperties().iterator();
                        propertyIterator.hasNext();) {
                    String startdate = null, enddate = null, 
                            event = null;
                    Property property = (Property) propertyIterator.next();
                    
                    if (property.getName().equals(
                            "X-APPLE-TRAVEL-ADVISORY-BEHAVIOR") 
                            || property.getName().equals(
                                    "X-APPLE-SUGGESTION-INFO-UNIQUE-KEY")
                            || property.getName().equals(
                                    "X-MS-OLK-AUTOFILLLOCATION")
                            || property.getName().equals(
                                    "X-MICROSOFT-DISALLOW-COUNTER")
                            || property.getName().equals(
                                    "X-APPLE-SUGGESTION-INFO-TIMESTAMP")
                            || property.getName().equals(
                                    "X-MICROSOFT-CDO-BUSYSTATUS")
                            || property.getName().equals(
                                    "TRANSP")
                            || property.getName().equals("UID")
                            || property.getName().equals(
                                    "X-MICROSOFT-CDO-IMPORTANCE")
                            || property.getName().equals("X-MS-OLK-CONFTYPE")
                            || property.getName().equals(
                                    "X-APPLE-STRUCTURED-DATA")
                            || property.getName().equals("LAST-MODIFIED")
                            || property.getName().equals("SEQUENCE")
                            || property.getName().equals(
                                    "X-APPLE-SUGGESTION-INFO-OPAQUE-KEY")
                            || property.getName().equals("ORGANIZER")
                            || property.getName().equals("CONFIRMED")
                            || property.getName().equals(
                                    "X-APPLE-STRUCTURED-LOCATION")
                            || property.getName().equals("PRIORITY")
                            || property.getName().equals("TZID")
                            || property.getName().equals(
                                    "X-APPLE-SUGGESTION-INFO-CHANGED-FIELDS")
                            || property.getName().equals("ATTENDEE")
                            || property.getName().equals("URL")
                            || property.getName().equals("CLASS")
                            || property.getName().equals("X-ALT-DESC")
                            || property.getName().equals("STATUS")
                            || property.getName().equals("")
                            || property.getName().equals("")
                            /*
                            || property.getName().equals("")
                            */
                            ) {
                        continue;
                    }
                    
                    uniquePropertyNames.add(property.getName());
                    
                    item.put(property.getName(), property.getValue());
                    
                    if ("DTSTART".equals(property.getName())) {
                        startdate = property.getValue();
                        info.put("startDate",
                                modifyDateLayoutOfIcs(startdate)
                        );
                    }
                    
                    if ("DTEND".equals(property.getName())) {
                        enddate = property.getValue();
                        info.put("endDate",
                                modifyDateLayoutOfIcs(enddate)
                        );
                    }
                    if ("SUMMARY".equals(property.getName())) {
                        event = property.getValue();
                        info.put("event", event);
                    }
                    if ("DESCRIPTION".equals(property.getName())) {
                        event = property.getValue();
                        info.put("desc", event);
                    }

                    if (!info.isEmpty()) {
                        if (info.get("event") != null) {
                            result2.put(info.get("event") + " "
                                    + (info.get("desc")!= null?
                                            ("(" + info.get("desc")
                                                    + ")" + " ") : "")
                                    + info.get("startDate")+ " "
                                    + info.get("endDate")
                                    , info.get("startDate"));
                            
                            if(info.get("startDate") != null
                                    && info.get("endDate") == null )
                            result3.put(info.get("event") + " "
                                    + (info.get("desc")!= null?
                                            ("(" + info.get("desc") + ")"
                                                    + " ") : "")
                                    + info.get("startDate")+ " "
                                    + info.get("endDate")
                                    , info.get("startDate"));
                            if(info.get("startDate") == null
                                    && info.get("endDate") != null)
                            result4.put(info.get("event") + " "
                                    + (info.get("desc")!= null?
                                            ("(" + info.get("desc") + ")"
                                                    + " ") : "")
                                    + info.get("startDate")+ " "
                                    + info.get("endDate")
                                    , info.get("startDate"));
                        }
                    }
                }
                items.add(item);
            }            

            System.out.println("added size: " + result2.size());
            System.out.println("added size NOT NULL start: " + result3.size());
            System.out.println("added size NOT NULL end: " + result4.size());
            
            System.out.println("items: " + items.size());
            int i = 0;
            for(Map<String, String> item: items) {
                i++;
                for (Map.Entry<String,String> entry: item.entrySet()) {
                    Object k = entry.getKey();
                    Object v = entry.getValue();
                    if (!(""+v).isEmpty())
                        //if(k.equals("STATUS"))
                            System.out.println(k + " " + v
                    );
                    //if (!(""+v).contains("MNTL")) {}
                            
                }
                System.out.println(i + "zzz");
                //Object k = ((Set)itm.keySet()).iterator().next();
                //Object v = ((Collection)itm.values()).iterator().next();
                ///System.out.println(k + " " + v);
                //}
                    
            }
            
            System.out.println("uniquePropertyNames: " + uniquePropertyNames);
            //for (Iterator i =
                      //calendar.getComponents().iterator(); i.hasNext();) {
             //   Component component = (Component) i.next();
                //System.out.println(component.getName());
            //}
        } catch (IOException | ParserException ex) {
            Logger.getLogger(iCalExport.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
        
        for(Map<String, String> itm: items) {
            String resultSummary = "";
            for (Map.Entry<String,String> entr: itm.entrySet()) {
                //Object k = entr.getKey();
                Object v = entr.getValue();
                resultSummary += v;
                resultSummary += " ";
            }
            System.out.println(resultSummary);
            importToJira(resultSummary);
        }
    }

    private static String modifyDateLayoutOfIcs(String inputDate) {
        try {
            SimpleDateFormat inDateFormat =
                    new SimpleDateFormat("yyyyMMdd'T'HHmmss");
            java.util.Date fromDate = inDateFormat.parse(inputDate);
            SimpleDateFormat outDateForm =
                    new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return outDateForm.format(fromDate);
        } catch (ParseException e) {
            return inputDate;
        }
    }
    
    private static void importToJira(final String summary) {
        try {
            JiraRestClient restClient = new AsynchronousJiraRestClientFactory()
                    .createWithBasicHttpAuthentication(
                            new URI(JIRA_URI),
                            JIRA_USERNAME,
                            JIRA_USERPASSWORD
                    );
            Long newFeature = 10003L;
            createIssue(restClient, "QUES", newFeature, summary);
        } catch (URISyntaxException ex) {
            Logger.getLogger(iCalExport.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
    }
    
    private static Map<String, String> exportFromJira() {
        try {
            JiraRestClient restClient = new AsynchronousJiraRestClientFactory()
                    .createWithBasicHttpAuthentication(
                            new URI(JIRA_URI),
                            JIRA_USERNAME,
                            JIRA_USERPASSWORD
                    );
            Issue issue1 = getIssue(restClient, "QUES", "1");
            Issue issue2 = getIssue(restClient, "QUES", "2");
            Map<String, String> map = new HashMap<>();
            //map.put(issue1.getKey(), issue1.getSummary());
            //map.put(issue2.getKey(), issue2.getSummary());
            map.put("ISS-1", "testTextSummary");
            return map;
        } catch (URISyntaxException | InterruptedException |
                ExecutionException ex) {
            Logger.getLogger(iCalExport.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
        return new HashMap<>();
    }
    
    public static String createIssue(
            JiraRestClient restClient, String projectKey,
            Long issueType, String issueSummary) {
        IssueRestClient issueClient = restClient.getIssueClient();
        IssueInput newIssue = new IssueInputBuilder(
          projectKey, issueType, issueSummary).build();

        return issueClient.createIssue(newIssue).claim().getKey();
    }
    
    public static Issue getIssue(
            JiraRestClient restClient,
            String projectId, String issueId) throws InterruptedException,
            ExecutionException {
        IssueRestClient issueClient = restClient.getIssueClient();
        //String projectId = "QUES";
        //String issueId = "1";
        Promise<Issue> issue = issueClient.getIssue(projectId + issueId);
        
        return issue.get();
    }
    
    private static Transition getTransitionByName(
            Iterable<Transition> transitions, String transitionName) {
        for (Transition transition : transitions) {
            if (transition.getName().equals(transitionName)) {
                return transition;
            }
        }
        return null;
    }
    
    public static class iCal {

        private String version =    "VERSION:2.0\r\n";
        private String prodid =     "PRODID://test/test2/test3/test4/314sum2015//"
                + "\r\n";
        private String calBegin =   "BEGIN:VCALENDAR\r\n";
        private String calEnd =     "END:VCALENDAR\r\n";
        private String eventBegin = "BEGIN:VEVENT\r\n";
        private String eventEnd =   "END:VEVENT\r\n";

        public void iCal(){
        }

        public void write(final String name, final Map<String, String> items) {
            StringBuilder builder = new StringBuilder();
            builder.append(name);
            try {
                File file = new File(builder.toString());

                // if file does not exists, then to create it
                if (!file.exists()) {
                    file.createNewFile();
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(calBegin);
                bw.write(version);
                bw.write(prodid);
                
                for (Map.Entry<String,String> entry: items.entrySet()) {
    
                    bw.write(eventBegin);
                
                    String testExample = "UID:me@exampleemailexample.com\r\nDTSTAMP:19970714T170000Z\r\nORGANIZER;"+
                    "CN=Name Name:MAILTO:name.name@exampleemailexample.com\r\nDTSTART:19970714T170000Z"+
                    "\r\nDTEND:19970715T035959Z\r\nSUMMARY:"
                            //+ "Bastille Day Party"
                            + entry.getValue()
                            + "\r\n";
                    bw.write(testExample);
                
                    bw.write(eventEnd);
                }
                
                bw.write(calEnd);
                bw.close();

                System.out.println("Done");
            } catch (IOException ex) {
                Logger.getLogger(iCalExport.class.getName())
                    .log(Level.SEVERE, null, ex);
            }
        }
    }
}

The update from 2021-02-10.

Here is the link to migrate from Atlassian Jira Server to Atlassian Jira Cloud: https://support.atlassian.com/migration/docs/migrate-from-server-to-cloud.

Also there is a quote from there: "We’ve ended sales for new server licenses, and will end support for server on February 2, 2024." as of 2021-02-10.

So that one will be also the expiry date for this article.

Expiry date: 2024-02-02.

Or there is for example JetBrains YouTrack as an alternative one.